Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add box-shadow for React-native? #709

Closed
jasan-s opened this issue Apr 19, 2017 · 37 comments
Closed

How to add box-shadow for React-native? #709

jasan-s opened this issue Apr 19, 2017 · 37 comments

Comments

@jasan-s
Copy link

jasan-s commented Apr 19, 2017

in essence:

shadowOffset:{  width: 10,  height: 10,  },
shadowColor: 'black',
shadowOpacity: 1.0,
@k15a
Copy link
Member

k15a commented Apr 19, 2017

Just like normal CSS: https://developer.mozilla.org/de/docs/Web/CSS/box-shadow

const BoxShadow = styled.div`
  box-shadow: 10px 5px 5px black;
`

@k15a k15a closed this as completed Apr 19, 2017
@k15a k15a added the question label Apr 19, 2017
@jasan-s
Copy link
Author

jasan-s commented Apr 19, 2017

@k15a I forgot to add that this is on react-native

@k15a k15a reopened this Apr 19, 2017
@mxstbr
Copy link
Member

mxstbr commented Apr 19, 2017

/cc @jacobp100 we should write some docs specifically for ReactNative on the new website. https://github.com/styled-components/styled-components-website

@jasan-s jasan-s changed the title How to add box-shadow? How to add box-shadow for React-native? Apr 19, 2017
@jasan-s
Copy link
Author

jasan-s commented Apr 19, 2017

For some reason @k15a @mxstbr the following two give me different results in terms of styling(equivalent property values):

const styles = StyleSheet.create({
  containerStyle: {
    borderWidth: 1,
    borderRadius: 2,
    borderColor: '#ddd',
    borderBottomWidth: 0,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.8,
    shadowRadius: 2,
    elevation: 1,
    marginLeft: 5,
    marginRight: 5,
    marginTop: 10,
  }
})
const StyledView = styled.View`
  border-width: 1;
  border-radius: 2;
  border-color: #ddd;
  border-bottom-width: 0;
  shadow-color: #000;
  shadow-offset: {width: 0, height: 2};
  shadow-opacity: 0.8;
  shadow-radius: 2;
  elevation: 1;
  margin-left: 5;
  margin-right: 5;
  margin-top: 10;
`

NOTICE THE DIFFERENT PLACEMENT OF SHADOW using styled-components the shadow is on top:

 (With Stylesheet create -- the shadow is on bottom)
(With styles-components -- the shadow is on top)

@jacobp100
Copy link
Contributor

jacobp100 commented Apr 19, 2017

You can use shadow-offset: 5 10 where the values are width and height, respectively. In v2, you'll need to add units to those values.

I think you got the rest!

@mxstbr I'll submit some stuff later tonight! We should be able to take a load from here.

@guysegal
Copy link

These props are relevant only to iOS right?

shadow-color, shadow-offset, shadow-opacity, shadow-radius

Will they work on Android?

@kitten
Copy link
Member

kitten commented Jul 12, 2017

@guysegal they won't since shadows are not supported on RN Android. But you can use elevation

@pie6k
Copy link

pie6k commented Jul 19, 2017

<View
      style={{
        shadowOpacity: 0.75,
        shadowRadius: 5,
        shadowColor: 'red',
        shadowOffset: { height: 0, width: 0 },
      }}
    >

@ColinFendrick
Copy link

Is this working? Trying to put some shadow on a view with the exact same above code and nothing is happening.

@jacobp100
Copy link
Contributor

It only works for iOS IIRC

@ColinFendrick
Copy link

We're building React Native for iOS and it's not showing up.

@jacobp100
Copy link
Contributor

Does it work with the native StyleSheet.create?

@kitten
Copy link
Member

kitten commented Aug 4, 2017

@ColinFendrick Does this definitely not work for you?

styled.View`
  shadow-opacity: 0.75;
  shadow-radius: 5px;
  shadow-color: red;
  shadow-offset: 0px 0px;
`

@ColinFendrick
Copy link

Figured out the problem. overflow: hidden; will ofc turn that box shadow off. Thanks.

@ikhattab
Copy link

This isn't working for me, I tried to add:

box-shadow: 10px 5px 5px black;

but it gives me the following warning:

Error:  Error: "boxShadow" is not a valid style property.
StyleSheet generated: {
  "borderBottomWidth": 1,
  "borderBottomColor": "#ebebeb",
  "display": "flex",
  "flexDirection": "row",
  "alignItems": "center",
  "backgroundColor": "#fff",
  "boxShadow": "10px 5px 5px black"
}

@jacobp100
Copy link
Contributor

@mxstbr @philpl Do we want to try and support a translation from box-shadow to various shadow-* properties?

@kitten
Copy link
Member

kitten commented Aug 24, 2017

@jacobp100 Yes! I'd love that ❤️ 😄

@dakdreams
Copy link

my emulator is android but the shadow props dont work!!!

@k15a
Copy link
Member

k15a commented Oct 19, 2017

@dakdreams AFAIK android doesn't support shadows.

Try elevation:

(Android-only) Sets the elevation of a view, using Android's underlying elevation API. This adds a drop shadow to the item and affects z-order for overlapping views. Only supported on Android 5.0+, has no effect on earlier versions.

@RyanWarner
Copy link

As @jasan-s pointed out, the shadows seem to be on the top instead of the bottom of the View. Any way around this?

@redbar0n
Copy link

redbar0n commented Mar 8, 2018

Upvote here: https://react-native.canny.io/feature-requests/p/shadow-does-not-appear-if-overflow-hidden-is-set-on-ios

@Nemesis30G
Copy link

I tried this
shadowOffset:{ width: 10, height: 10, },
shadowColor: 'black',
shadowOpacity: 1.0,

but I have some text inside the view and it make the text have shadow too, any way around this?

@SSTPIERRE2
Copy link

SSTPIERRE2 commented Apr 2, 2018

I just tried wrapping a scrollview with text in a view styled with shadow props, but as @Nemesis30G said the shadow actually applied to the text inside the scrollview as well as the borders of the view.

@jacobp100
Copy link
Contributor

Set a background colour on the scrollview

@risingagain
Copy link

This is different than CSS style.

@risingagain
Copy link

Inset is not working.

@jacobp100
Copy link
Contributor

inset is not supported by React native

@Karniej
Copy link

Karniej commented Jul 5, 2018

The issue with shadowOffset should be documented a little bit better. The only way to work with shadowOffset on React Native was by using the shorthand property
boxShadow: 1px 10px 10px black

@LetsNurtureGit
Copy link

elevation:4,
shadowOffset: { width: 5, height: 5 },
shadowColor: "grey",
shadowOpacity: 0.5,
shadowRadius: 10,

Dont forget to put margin and padding efficiently as the shadows wont fall out of the container view in android.. However in iOS it shudn't be an issue

@rickardinho
Copy link

rickardinho commented Aug 4, 2018

So is anybody going to answer why shadows are appearing on top on IOS, no matter what the shadowOffset properties, as @jasan-s pointed out?

@Kishanjvaghela

This comment has been minimized.

@rajnishcoder
Copy link

elevation:4,
shadowOffset: { width: 5, height: 5 },
shadowColor: "grey",
shadowOpacity: 0.5,
shadowRadius: 10,

Dont forget to put margin and padding efficiently as the shadows wont fall out of the container view in android.. However in iOS it shudn't be an issue

all above solutions doesn't worked in android but this works!!! 😃

@Kishanjvaghela
Copy link

@skhetcho
Copy link

skhetcho commented Oct 20, 2018

Just like normal CSS: https://developer.mozilla.org/de/docs/Web/CSS/box-shadow

const BoxShadow = styled.div`
  box-shadow: 10px 5px 5px black;
`

what is this syntax @k15a
as we are used to with react native to have:

style: {
     //some react native css info
}

@bradennapier
Copy link

This seems to be working fine for me:

const IconImage = styled.Image`
  height: 30px;
  width: 30px;
  shadow-color: #000;
  shadow-offset: 1px 1px;
  shadow-opacity: 0.5;
  shadow-radius: 2px;
`;

@jacoblukose
Copy link

jacoblukose commented Oct 21, 2018

I tried this
shadowOffset:{ width: 10, height: 10, },
shadowColor: 'black',
shadowOpacity: 1.0,

but I have some text inside the view and it make the text have shadow too, any way around this?

Hi is there a solution for this ? All my inner view elements ( text , image ) are also havings shadows

@LetsNurtureGit
Copy link

elevation:4,
shadowOffset: { width: 5, height: 5 },
shadowColor: "grey",
shadowOpacity: 0.5,
shadowRadius: 10,
Dont forget to put margin and padding efficiently as the shadows wont fall out of the container view in android.. However in iOS it shudn't be an issue

all above solutions doesn't worked in android but this works!!! 😃

thanks

@styled-components styled-components locked as resolved and limited conversation to collaborators Nov 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests