DEV Community

Cover image for React-Shadow-Component: Add Shadows to Your Components with Ease!
Jerry Nwosu
Jerry Nwosu

Posted on

React-Shadow-Component: Add Shadows to Your Components with Ease!

React-Shadow-Component is an easy-to-use library for adding shadows to your React components. Whether you're creating a website, a mobile app, or any other type of user interface, the ability to add shadows to your components can greatly enhance the visual appeal of your project.

The library has two main export components, BoxShadow and WithShadow, that make it simple to add shadows to your components. In this article, we'll take a closer look at how to use these components to add shadows to your React components.

Installation

To install React-Shadow-Component, simply run the following command in your terminal:

npm install react-shadow-component
Enter fullscreen mode Exit fullscreen mode

or if you prefer Yarn:

yarn add react-shadow-component
Enter fullscreen mode Exit fullscreen mode

BoxShadow Component

The BoxShadow component is used to add a box shadow to your components. It accepts several props, including color, offsetX, offsetY, blur, and spread. These props allow you to customize the shadow to fit your design needs.

The BoxShadow component takes precedence over the WithShadow component when both are used. This means that if you pass props to the BoxShadow component, it will use those props instead of the WithShadow props.

Here's an example of how to use the BoxShadow component:

import React from 'react';
import { BoxShadow } from 'react-shadow-component';

export const MyComponent = () => {
  return (
    <BoxShadow color="#000" offsetX={10} offsetY={10} blur={10} spread={10}>
      <div>My Shadowed Component</div>
    </BoxShadow>
  );
};

Enter fullscreen mode Exit fullscreen mode

WithShadow Component

The WithShadow component is used to wrap any element and add a shadow to it. If the BoxShadow component does not have any props, it will use the props from the WithShadow component if provided.

Here's an example of how to use the WithShadow component:

import React from 'react';
import { WithShadow, BoxShadow } from 'react-shadow-component';

export const MyComponent = () => {
  return (
    <WithShadow color="#000" offsetX={10} offsetY={10} blur={10} spread={10}>
      // This color overrides the color
      // passed to `WithShadow`
      <BoxShadow color="#ff0000">
        <div>My Shadowed Component</div>
      </BoxShadow>
      <BoxShadow>
        <div>My Shadowed Component</div>
      </BoxShadow>
    </WithShadow>
  );
};

Enter fullscreen mode Exit fullscreen mode

In this example, we import the WithShadow component from the react-shadow-component library. Then, we wrap the component we want to add a shadow to with the WithShadow component. Finally, we pass the props we want to use to the WithShadow component. In this case, we're using the color, offsetX, offsetY, blur, and spread props to customize the shadow.

Default Props

React-Shadow-Component also has default props, so you don't have to worry about passing props to the components every time (If you prefer the default shadow πŸ˜‰). However, if you pass props to the BoxShadow component, it will take precedence over the default props and the WithShadow props. If you pass props to the WithShadow component, it will take precedence over the default props, but only if the BoxShadow component does not have any props.

Got it πŸ€·β€β™‚οΈ? If not, don't worry! Just imagine you're playing a game of shadows where BoxShadow is the big boss, WithShadow is the lieutenant, and the default props are just the little minions following orders. If the big boss says jump, they jump, if the lieutenant says sit, they sit πŸ˜‚.

Conclusion

React-Shadow-Component is a fun and effective way to add shadows to your React components. With its two main components, BoxShadow and WithShadow, and its ability to customize the shadow through props, you have full control over the look of your shadows. So go ahead, give it a try and turn your flat and boring components into a work of shadow art!

You can find the library on Github here: React-Shadow-Component πŸš€

Top comments (2)

Collapse
 
naucode profile image
Al - Naucode

Hey, that was a nice read, you got my follow, keep writing πŸ˜‰

Collapse
 
geekreflex profile image
Jerry Nwosu

Thank you!