concept drop shadow in category react native

This is an excerpt from Manning's book React Native in Action.
In this section, you’ll go beyond the basics of changing font styles and start applying decorative styles to text. I’ll show you how to do things like underline and strikethrough text and add drop shadows. These techniques can add a lot of visual variety to applications and help text elements stand out from one another.
Chapter 4 introduced styling React Native components. It showed how to style
ViewandTextcomponents, styles you’ll likely use every day and that mostly affect the look of a component. This chapter continues the discussion and goes into more depth with platform-specific styles; drop shadows; manipulating components with transformations such as translation, rotation, scaling, and skewing; and dynamically laying out components with flexbox.
Let’s go back to the
ProfileCardexample from the last chapter and add some drop shadows that will look great on iOS and not so great on Android. You’ll add a drop shadow to the entireProfileCardcontainer and to the circular image container. Figure 5.4 shows what you’re shooting for on iOS and what you’ll get on Android.Notice that even with
elevationapplied on Android, you don’t see much of a shadow. The reality is, on Android you’ll never get close to the shadow effects that can be produced on iOS with React Native out of the box. If you really must have drop shadows on Android, then I suggest looking for a component on npm or yarn that does what you need. Experiment with different components, and see if you can get the Android version looking as sharp as the iOS version. I don’t have any recommendations; I stay away from drop shadows or accept the differences.Figure 5.4 The
ProfileCardon iOS and Android after drop shadows have been added to the card container and the circular image container. The drop shadows on iOS are created using the iOS-specific shadow properties:shadowColor,shadowOffset, andshadowOpacity. On Android, theelevationproperty is used to try to create depth. It produces only a minor shadow effect, far inferior to the shadows produced on iOS.![]()
The code in this chapter begins with listing 4.20: the completed
ProfileCardexample from chapter 4. Listing 5.1 only shows the changes needed to apply the drop shadows to the component. You don’t need to add a lot of code to get the drop shadows on iOS. Look at the listing on an Android device, and see how theelevationsetting causes the faintest of shadows.Listing 5.1 Adding drop shadows to the
ProfileCardimport React, { Component } from 'react'; import { Image, Platform, StyleSheet, Text, View} from 'react-native'; #1 ... cardContainer: { ... height: 400, ...Platform.select({ #2 ios: { shadowColor: 'black', shadowOffset: { height: 10 }, shadowOpacity: 1 }, android: { elevation: 15 } }) }, cardImageContainer: { ... paddingTop: 15, ...Platform.select({ #3 ios: { shadowColor: 'black', shadowOffset: { height: 10, }, shadowOpacity: 1 }, android: { borderWidth: 3, borderColor: 'black', elevation: 15 } }) }, ... #1 Imports the Platform utility component to programmatically select styles based on the platform #2 Adds a drop shadow to the card container based on the platform #3 Adds a drop shadow to the circular image container !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":16},{\"line\":1,\"ch\":24}],[{\"line\":6,\"ch\":5},{\"line\":6,\"ch\":13}],[{\"line\":22,\"ch\":5},{\"line\":22,\"ch\":13}],[{\"line\":39,\"ch\":16},{\"line\":39,\"ch\":24}],[{\"line\":6,\"ch\":2},{\"line\":6,\"ch\":22}],[{\"line\":22,\"ch\":2},{\"line\":22,\"ch\":22}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":10}],[{\"line\":23,\"ch\":4},{\"line\":23,\"ch\":10}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":27}],[{\"line\":24,\"ch\":6},{\"line\":24,\"ch\":27}],[{\"line\":9,\"ch\":6},{\"line\":9,\"ch\":21}],[{\"line\":25,\"ch\":6},{\"line\":25,\"ch\":21}],[{\"line\":10,\"ch\":8},{\"line\":10,\"ch\":18}],[{\"line\":26,\"ch\":8},{\"line\":26,\"ch\":18}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":8}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":6}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":8}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":6}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":12,\"ch\":6},{\"line\":12,\"ch\":22}],[{\"line\":28,\"ch\":6},{\"line\":28,\"ch\":22}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":8}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":6}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":8}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":6}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":14,\"ch\":4},{\"line\":14,\"ch\":14}],[{\"line\":30,\"ch\":4},{\"line\":30,\"ch\":14}],[{\"line\":15,\"ch\":6},{\"line\":15,\"ch\":19}],[{\"line\":33,\"ch\":6},{\"line\":33,\"ch\":19}],[{\"line\":0,\"ch\":26},{\"line\":0,\"ch\":27}],[{\"line\":1,\"ch\":48},{\"line\":1,\"ch\":49}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":7}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":5}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":5}],[{\"line\":17,\"ch\":2},{\"line\":17,\"ch\":3}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":1}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":7}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":5}],[{\"line\":34,\"ch\":4},{\"line\":34,\"ch\":5}],[{\"line\":35,\"ch\":2},{\"line\":35,\"ch\":3}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":1}],[{\"line\":17,\"ch\":2},{\"line\":17,\"ch\":4}],[{\"line\":35,\"ch\":2},{\"line\":35,\"ch\":4}],[{\"line\":6,\"ch\":2},{\"line\":6,\"ch\":22}],[{\"line\":22,\"ch\":2},{\"line\":22,\"ch\":22}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":10}],[{\"line\":23,\"ch\":4},{\"line\":23,\"ch\":10}],[{\"line\":8,\"ch\":6},{\"line\":8,\"ch\":27}],[{\"line\":24,\"ch\":6},{\"line\":24,\"ch\":27}],[{\"line\":9,\"ch\":6},{\"line\":9,\"ch\":21}],[{\"line\":25,\"ch\":6},{\"line\":25,\"ch\":21}],[{\"line\":26,\"ch\":8},{\"line\":26,\"ch\":19}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":8}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":6}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":8}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":6}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":12,\"ch\":6},{\"line\":12,\"ch\":22}],[{\"line\":28,\"ch\":6},{\"line\":28,\"ch\":22}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":8}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":6}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":2}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":8}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":6}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":2}],[{\"line\":14,\"ch\":4},{\"line\":14,\"ch\":14}],[{\"line\":30,\"ch\":4},{\"line\":30,\"ch\":14}],[{\"line\":31,\"ch\":6},{\"line\":31,\"ch\":21}],[{\"line\":32,\"ch\":6},{\"line\":32,\"ch\":27}],[{\"line\":15,\"ch\":6},{\"line\":15,\"ch\":19}],[{\"line\":33,\"ch\":6},{\"line\":33,\"ch\":19}],[{\"line\":0,\"ch\":26},{\"line\":0,\"ch\":27}],[{\"line\":1,\"ch\":48},{\"line\":1,\"ch\":49}],[{\"line\":11,\"ch\":6},{\"line\":11,\"ch\":7}],[{\"line\":13,\"ch\":4},{\"line\":13,\"ch\":5}],[{\"line\":16,\"ch\":4},{\"line\":16,\"ch\":5}],[{\"line\":17,\"ch\":2},{\"line\":17,\"ch\":3}],[{\"line\":18,\"ch\":0},{\"line\":18,\"ch\":1}],[{\"line\":27,\"ch\":6},{\"line\":27,\"ch\":7}],[{\"line\":29,\"ch\":4},{\"line\":29,\"ch\":5}],[{\"line\":34,\"ch\":4},{\"line\":34,\"ch\":5}],[{\"line\":35,\"ch\":2},{\"line\":35,\"ch\":3}],[{\"line\":36,\"ch\":0},{\"line\":36,\"ch\":1}],[{\"line\":17,\"ch\":2},{\"line\":17,\"ch\":4}],[{\"line\":35,\"ch\":2},{\"line\":35,\"ch\":4}]]"} !@%STYLE%@!Just as with font selection in chapter 4, you use the
Platform.selectfunction to apply different styles to components based on the platform: iOS or Android. In some cases, like the drop shadow, one platform may perform much better than the other; but in most cases the styles will behave the same on both platforms, which is an amazing benefit of React Native.