Effective Styling Techniques Using styled-components and CSS Function

This article introduces effective styling techniques using styled-components and CSS function in React applications.

`styled-components` is a powerful library that allows you to write CSS in JavaScript code within React applications. This approach enables a closer integration between CSS and React components, enhancing modularity and maintainability of styles. Specifically, using the `css` function facilitates conditional styling, reusable style fragments, and nested styles more easily. This article delves into the basic usage and practical examples of `styled-components` and the `css` function.

Installation

First, you need to install `styled-components`. Run the following command using npm or yarn:

npm install styled-components
or
yarn add styled-components

Basic Usage

You can define styles using `styled-components` and apply them to React components. For example, here’s how to define basic button styles:

import styled from 'styled-components';

const Button = styled.button`
  background: palevioletred;
  border-radius: 3px;
  border: none;
  color: white;
  padding: 0.5em 1em;
  font-size: 1em;
  cursor: pointer;

  &:hover {
    background: darkviolet;
  }
`;

const App = () => (
  <div>
    <Button>Click Me</Button>
  </div>
);

export default App;

Role and Usage of the css Function

The `css` function in `styled-components` allows you to write styles in a more flexible and reusable way, enhancing modularity and maintainability.

Conditional Styling

Using the `css` function, you can easily write conditional styling. For instance, it’s useful when you want to apply different styles based on the button type.

import styled, { css } from 'styled-components';

const buttonStyles = css`
  background: ${props => props.primary ? 'palevioletred' : 'white'};
  color: ${props => props.primary ? 'white' : 'palevioletred'};
`;

const Button = styled.button`
  ${buttonStyles}
  border: 2px solid palevioletred;
  border-radius: 3px;
  padding: 0.5em 1em;
  font-size: 1em;
  cursor: pointer;

  &:hover {
    background: ${props => props.primary ? 'darkviolet' : 'lightgray'};
  }
`;

const App = () => (
  <div>
    <Button>Normal Button</Button>
    <Button primary>Primary Button</Button>
  </div>
);

export default App;

Reusing Style Fragments

You can define style fragments that can be reused across multiple components using the `css` function, reducing code duplication and maintaining consistency.

import styled, { css } from 'styled-components';

const sharedStyles = css`
  border-radius: 3px;
  padding: 0.5em 1em;
  font-size: 1em;
  cursor: pointer;
`;

const Button = styled.button`
  ${sharedStyles}
  background: palevioletred;
  color: white;
  border: none;

  &:hover {
    background: darkviolet;
  }
`;

const Input = styled.input`
  ${sharedStyles}
  border: 2px solid palevioletred;
  color: palevioletred;
`;

const App = () => (
  <div>
    <Button>Click Me</Button>
    <Input placeholder="Type here..." />
  </div>
);

export default App;

Nested Styles

The `css` function also allows you to define nested styles, making complex styling more concise and readable.

import styled, { css } from 'styled-components';

const nestedStyles = css`
  &:hover {
    color: darkviolet;
    span {
      text-decoration: underline;
    }
  }
`;

const StyledLink = styled.a`
  color: palevioletred;
  ${nestedStyles}
`;

const App = () => (
  <div>
    <StyledLink href="#">
      Hover over me <span>and underline me!</span>
    </StyledLink>
  </div>
);

export default App;

Using Themes

By using `ThemeProvider`, you can set and apply themes to styles, which is useful for applying consistent styles across the entire application.

import styled, { ThemeProvider } from 'styled-components';

const theme = {
  primary: 'palevioletred',
  secondary: 'darkviolet',
};

const Button = styled.button`
  background: ${props => props.theme.primary};
  color: white;
  padding: 0.5em 1em;
  border: none;
  border-radius: 3px;
  font-size: 1em;
  cursor: pointer;

  &:hover {
    background: ${props => props.theme.secondary};
  }
`;

const App = () => (
  <ThemeProvider theme={theme}>
    <div>
      <Button>Themed Button</Button>
    </div>
  </ThemeProvider>
);

export default App;

Defining Global Styles

You can define global styles using `createGlobalStyle`, which is useful for setting the basic styles of the application.

import { createGlobalStyle } from 'styled-components';

const GlobalStyle = createGlobalStyle`
  body {
    margin: 0;
    padding: 0;
    font-family: 'Arial', sans-serif;
  }
`;

const App = () => (
  <>
    <GlobalStyle />
    <div>
      <h1>Hello World</h1>
    </div>
  </>
);

export default App;

Conclusion

`styled-components` is a powerful tool that allows for a closer integration of CSS and React components through the CSS-in-JS paradigm. The `css` function enables writing styles in a more flexible and reusable manner, making it very useful for conditional styling, reusable style fragments, and nested styles. This approach helps modularize styling and improves maintainability. Refer to the examples above to apply your own styles!

답글 남기기