• Stars
    star
    621
  • Rank 70,540 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 27 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-platform shadow for React Native. Supports Android, iOS, Web, and Expo

npm TypeScript npm PRs Welcome

react-native-shadow-2

react-native-shadow is 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 appearence and usage for Android, iOS and Web.

The ethercreative/react-native-shadow-generator website won't give you very similar results between the two platforms, for the reasons I described here. It's also not as customizable as this library.

Compatible with Android, iOS and Web. And Expo!

Supports RTL.

🍟 Demo - Expo Snack Sandbox

Give this library a quick try!

📰 v7 Changelog - 2022-08-08

There are important breaking changes! Read it if upgrading!

↪️ Pre v7 Readme

Old Readme, if you are still using previous versions.

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 fallbacks to true. Explanation. Record<'start' | 'end' | 'top' | 'bottom', boolean> undefined
corners The corners that will have their shadows drawn. Undefined corners fallbacks 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 ocuppy 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 children'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 corners radii, although I recommend the latter as it's the RTL standard.

  • Having a radius greater than its side will mess 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!

    It's exported the ShadowProps type, the props of the Shadow component. You may do the following:

    const ShadowPresets = {
      button: {
        offset: [0, 1], distance: 1, startColor: '#0003',
      } as ShadowProps,
    };
    
    <Shadow {...ShadowPresets.button}>
  • The offset and size properties are throwing Typescript errors!

    Upgrade your Typescript to at least 4.0.0! Those two properties use labeled tuples. If you don't use Typescript, this won't happen.

📰 Popularly seen on

More Repositories

1

eslint-config-biome

Disables ESLint rules that have a recommended and equivalent Biome rule
JavaScript
73
star
2

GitHub-Repository-Manager

VS Code Extension to quickly clone, initialize and open your GitHub repositories
TypeScript
30
star
3

pagescrollview

React Native ScrollView with common settings and bugs fixes
TypeScript
21
star
4

react-native-payment-icons

Add Credit Card and other Payment Methods icons to your React Native app!
TypeScript
17
star
5

RPi-12864-LCD-ST7920-lib

An English translation and improvement from this original Czech code: http://www.astromik.org/raspi/42.htm
Python
13
star
6

eslint-config-gev

My curated ESLint rules
JavaScript
6
star
7

expo-font-loader

TypeScript
5
star
8

firebase-database-modeler

Take your Firebase Realtime Database to the next level with models!
TypeScript
4
star
9

firebase-functions-extended

Extended onCall function for Firebase Cloud Functions
TypeScript
4
star
10

gev

Effortlessly npx way to init slightly opinionated projects boilerplates
TypeScript
4
star
11

Logiwin

An old Logisim project that implemented a computer from a logic circuit. Spent some years doing it. Stopped when reached the software level.
4
star
12

react-native-gev

TypeScript
3
star
13

react-native-shadow-3

JavaScript
3
star
14

magneticDeclinationByCoord

Wants to convert your GPS coordinates to the magnetic declination? AUTOMATICALLY? Concerned about the accuracy of it? OR NOT? 3AM here and I finished it!
Objective-C
2
star
15

PDA-Minerva-Rockets

[Old project! For the new one, open my "pmm"!] A telemetry system by RF I made for my college rocketry team. Designed to work with a Raspberry Pi 3 and a LoRa RFM95
Python
2
star
16

fractional-indexing-utils

Common functionalities when using fractional indexing
2
star
17

expo-image-picker-hook

TypeScript
2
star
18

Circuiteer

Logic circuit expression reduction and builder
C
1
star
19

SKUcon-Decomposer

Simple npm package to decompose my SKU naming convention, the SKUcon.
TypeScript
1
star
20

ig-store-saved

Stores locally all the saved media of an Instagram account.
TypeScript
1
star
21

typedoc2md

https://github.com/tgreyuk/typedoc-plugin-markdown/issues/59#issuecomment-869108999
1
star
22

bitburner

TypeScript
1
star
23

mdol

Markdown value replacer. Simple alternative to Handlebars, replaces values directly on .md.
TypeScript
1
star