React-native-web: StyleSheet: add support for 'boxShadow'

Created on 14 Dec 2015  路  9Comments  路  Source: necolas/react-native-web

Not sure what to do about this. React Native adds shadow properties:

shadowOffset
shadowOpacity
shadowRadius

Web has box-shadow, and it supports multiple shadows.

color
inset
offsetX
offsetY
spreadRadius

I can't see a clear mapping between the two. At least for now we can add boxShadow: string as another web-specific style prop.

Most helpful comment

I'm going to fudge this for web since it seems to be used quite a lot.

All 9 comments

@necolas is this working? My styles don't seem to be coming through on web. I'm using a custom made shadow conversion util:

export function shadow({ color, opacity, radius, offsetWidth, offsetHeight, elevation }) {
  return Platform.select({
    ios: {
      shadowColor: color,
      shadowOpacity: opacity,
      shadowRadius: radius,
      shadowOffset: {
        width: offsetWidth,
        height: offsetHeight,
      }
    },
    android: {
      elevation
    },
    web: {
      boxShadow: `${offsetWidth}px ${offsetHeight}px ${radius}px ${rgba}`
    }
  });
}

Looks like rgba isn't defined?

@necolas it was, above that function.

const rgba = (color, opacity) => colors.opacity(color, opacity);

where colors is imported above.

@necolas I updated my comment above. I'm testing our project side-by-side in simulator and in the browser and I think some caching was happening with hot-loading. I thought I had figured out the issue but I hadn't.

If you can provide a test case and/or what the browser is showing in the dev tools, that would help

I've got a stateless, functional component that returns a<View style={styles.dishShadow} /> with children components. The dishShadow style is computed with the following (I've condensed here but the helper functions are imported). colors is also imported.

const rgba = (color, opacity) => colors.opacity(color, opacity);
const shadow = ({ color, opacity, radius, offsetWidth, offsetHeight, elevation }) => {
  return Platform.select({
    ios: {
      shadowColor: color,
      shadowOpacity: opacity,
      shadowRadius: radius,
      shadowOffset: {
        width: offsetWidth,
        height: offsetHeight,
      }
    },
    android: {
      elevation
    },
    web: {
      boxShadow: `${offsetWidth}px ${offsetHeight}px ${radius}px ${rgba}`
    }
  });
};

const styles = StyleSheet.create({
  dishShadow: {
    borderRadius: 3,
    ...shadow({
      elevation: 3,
      color: 'rgba(2,0,0,1)',
      opacity: 0.15,
      radius: 2,
      offsetWidth: 0,
      offsetHeight: 1
    })
  }
});

Output in native has the correct shadow. Output in web does not. In Chrome dev tools, the div that react-native-web creates does have border-radius: 3px on it, but no box-shadow styles.

image

rgba is a function but you haven't passed it any values. It looks like it should be:

      boxShadow: `${offsetWidth}px ${offsetHeight}px ${radius}px ${rgba(color, opacity)}`

I'm going to fudge this for web since it seems to be used quite a lot.

@necolas it was, above that function.

const rgba = (color, opacity) => colors.opacity(color, opacity);

where colors is imported above.

Where does colors is imported from?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mactub07 picture Mactub07  路  3Comments

necolas picture necolas  路  3Comments

blairio picture blairio  路  3Comments

EvanBacon picture EvanBacon  路  3Comments

holmesal picture holmesal  路  3Comments