Skip to content
프로젝트

ftzi/react-native-shadow-2

GitHub에서 보기

소개

🥷 React Native용 크로스 플랫폼 그림자입니다. Android, iOS, Web, Expo를 지원합니다.

웹 및 앱 개발

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
-1
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

react-native-shadow-2

npm npm PRs Welcome

react-native-shadow has been dead for years. This is an improved version with more functionalities, TypeScript support, and written from scratch. It's not required to define its size: the shadow is smartly applied on the first render and then precisely reapplied on the following ones.

It solves the old React Native issue of not having the same shadow appearance and usage for Android, iOS, and Web.

Compatible with Android, iOS and Web. And Expo!

Supports RTL.


🍟 Demo - Expo Snack Sandbox

Give this library a quick try!

📰 Changelog

❗ Read the FAQ below!

💿 Installation

1. First, install react-native-svg.

The latest react-native-svg version is recommended, including if using Expo.

2. Then install react-native-shadow-2:

npm i react-native-shadow-2
# or
yarn add react-native-shadow-2

📖 Usage

import { Shadow } from 'react-native-shadow-2';

<Shadow>
  <Text style={{ margin: 20, fontSize: 20 }}>🙂</Text>
</Shadow>

Example 1


<Shadow distance={15} startColor={'#eb9066d8'} endColor={'#ff00ff10'} offset={[3, 4]}>
  <View style={{ borderTopStartRadius: 24, borderBottomEndRadius: 0, borderRadius: 10, backgroundColor: '#c454f0dd' }}>
    <Text style={{ margin: 20, fontSize: 20 }}>🤯</Text>
  </View>
</Shadow>

Example 2

Properties

All properties are optional.

Property Description Type Default
startColor The initial gradient color of the shadow. string '#00000020'
endColor The final gradient color of the shadow. string Transparent startColor. Explanation.
distance How far the shadow goes. number 10
offset Moves the shadow. Negative x moves it left/start, negative y moves it up.

Accepts 'x%' values.

Defining this will default paintInside to true, as it's the usual desired behaviour.
[x: string | number, y: string | number] [0, 0]
paintInside Apply the shadow below/inside the content. startColor is used as fill color, without a gradient.

Useful when using offset or if your child has some transparency.
boolean false, but true if offset is defined
sides The sides that will have their shadows drawn. Doesn't include corners. Undefined sides fall back to true. Explanation. Record<'start' | 'end' | 'top' | 'bottom', boolean> undefined
corners The corners that will have their shadows drawn. Undefined corners fall back to true. Explanation. Record<'topStart' | 'topEnd' | 'bottomStart' | 'bottomEnd', boolean> undefined
style The style of the View that wraps your children. Read the Notes below. StyleProp<ViewStyle> undefined
containerStyle The style of the View that wraps the Shadow and your children. Useful for margins. StyleProp<ViewStyle> undefined
stretch Make your children occupy all available horizontal space. Explanation. boolean false
safeRender Won't use the relative sizing and positioning on the 1st render but on the following renders with the exact onLayout sizes. Useful if dealing with radii greater than the sides sizes (like a circle) to avoid visual artifacts on the 1st render.

If true, the Shadow won't appear on the 1st render.
boolean false
disabled Disables the Shadow. Useful for easily reusing components as sometimes shadows are not desired.

containerStyle and style are still applied.
boolean false

Notes

  • If the Shadow has a single child, it will get the width, height and all of the borderRadius properties from the child's style property, if defined.

  • You may also define those properties in the Shadow's style. The defined properties here will have priority over the ones defined in the child's style.

  • If the width and height are defined in any of those, there will be only a single render, as the first automatic sizing won't happen, only the precise render.

  • You can use either the 'borderTopLeftRadius' or 'borderTopStartRadius' and their variations to define the corner radii, although I recommend the latter as it's the RTL standard.

  • Having a radius greater than its side will mess up the shadow if the sizes aren't defined. You can use the safeRender property to only show the shadow on the 2nd render and beyond, when we have the exact component size and the radii are properly limited.

  • Radii greater than 2000 (Tailwind's rounded-full is 9999) may crash Android.

⁉️ FAQ

  • How to set the Shadow opacity?

    The opacity is set directly in the startColor and endColor properties, in the alpha channel. E.g.: '#0001' is an almost transparent black. You may also use '#rrggbbaa', 'rgba()', 'hsla()' etc. All patterns in this link, but not int colors, are accepted.

  • My component is no longer using the available parent width after applying the Shadow! What to do?

    Use the stretch property or style={{alignSelf: 'stretch'}} in your Shadow component. Explanation!

  • I want a preset for my Shadows!

    The ShadowProps type, the props of the Shadow component, is exported. You may do the following:

    const ShadowPresets = {
      button: {
        offset: [0, 1], distance: 1, startColor: '#0003',
      } as ShadowProps,
    };
    
    <Shadow {...ShadowPresets.button}>
    
  • The offset property is throwing TypeScript errors!

    Upgrade your TypeScript to at least 4.0.0! This property uses labeled tuples. If you don't use TypeScript, this won't happen.

📰 Popularly seen on

관련 프로젝트 살펴보기

불러오는 중…