Skip to content
tezvyn:
Menu

Cross-platform shadow with Platform API

Source: interviewEasyHow cards are made

Summary

platform-conditional styling.

Key points

iOS uses shadowColor/Offset/Opacity/Radius, Android uses elevation; merge both via Platform.select inside a style.

Watch out for

setting only shadow props and expecting a shadow on Android.

What's really being asked

Whether you know that iOS and Android implement elevation effects with completely different style properties and can write conditional styling cleanly with the Platform API.

The full answer

On iOS a shadow comes from four properties: shadowColor, shadowOffset, shadowOpacity, and shadowRadius. Android ignores those and instead uses a single numeric elevation, which simultaneously sets the drop shadow and the stacking order. Because the property sets do not overlap, you use Platform.select to supply the correct keys per platform within one StyleSheet entry, often combined with a backgroundColor since Android elevation needs an opaque background to render. This keeps both platforms styled from a single component without separate files.

The mistakes people make

Setting only shadow props and expecting Android to show a shadow, or only elevation and expecting iOS to. Forgetting that Android elevation requires a solid background to appear, or hardcoding values that look heavy on one platform, are practical mistakes.

What usually comes next

Why does Android elevation also affect z-index? How do you keep visual parity when the two shadow models look different? When would you reach for a library to unify shadows?

A concrete example

const styles = StyleSheet.create({ card: { backgroundColor: '#fff', borderRadius: 8, ...Platform.select({ ios: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.2, shadowRadius: 4 }, android: { elevation: 4 } }) } }); On iOS the four shadow props render the card shadow; on Android elevation 4 produces a comparable shadow and lifts the card above siblings, all from one style object.

Interview question

A card sets shadowRadius and shadowOpacity but shows no shadow on Android. What is the cause?

  • a.The StyleSheet must be recreated on every render for Android shadows to apply
  • b.Android ignores iOS shadow props and needs the elevation property insteadCorrect
  • c.Android shadows require wrapping the card in a SafeAreaView
  • d.shadowRadius must be a string with a px unit on Android
Why?

Android does not honor iOS shadow properties; it renders depth via the numeric elevation prop. The other options invent requirements that do not exist, so they are incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → reactnative.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles