Tips for creating more efficient styled components with repetitive styles

Utilizing Styled Components for styling in my project has resulted in numerous icons being defined. The style file contains the following code:

my-component.styles.ts

import { ReactComponent as CloseIcon } from 'svg/close.svg';
import { ReactComponent as OpenIcon } from 'svg/open.svg';
import { ReactComponent as DeleteIcon } from 'svg/delete.svg';
import { ReactComponent as CheckIcon } from 'svg/check.svg';
...

export const StyledCloseIcon = styled(CloseIcon)`
  width: 20px;
  fill: white;
`;

export const StyledOpenIcon = styled(OpenIcon)`
  width: 20px;
  fill: white;
`;

export const StyledDeleteIcon = styled(DeleteIcon)`
  width: 20px;
  fill: white;
`;

export const StyledCheckIcon = styled(CheckIcon)`
  width: 20px;
  fill: white;
`;
...

The above shows that all icons share the same styling attributes.

In another component, these icons are imported and used like this:

import {
  StyledCloseIcon,
  StyledOpenIcon,
  StyledDeleteIcon,
  StyledCheckIcon
} from './my-component.styles';

followed by using the icon components individually like <StyledCloseIcon />.

Is there a more concise way to implement this?

Answer №1

To avoid repetition, you can utilize mixins in your styled-components:

import { css } from 'styled-components';

const iconStyles = css`
  width: 20px;
  fill: white;
`;

export const StyledCloseIcon = styled(CloseIcon)`
  ${iconStyles}
`;

export const StyledOpenIcon = styled(OpenIcon)`
  ${iconStyles}
`;

export const StyledDeleteIcon = styled(DeleteIcon)`
  ${iconStyles}
`;

export const StyledCheckIcon = styled(CheckIcon)`
  ${iconStyles}
`;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

refreshing MySQL data without the need to reload the page

I am currently working on creating a universal "like button" that updates a MySQL table after being clicked without the need to reload the page. I believe this task can be achieved using JavaScript, although I am unsure of how to interact with MySQL usin ...

The Auth0 getTokenSilently function is not returning any value

I am currently working with the react-auth-spa.jsx file, /* eslint-disable react/require-default-props */ // src/react-auth0-spa.js import React, { useState, useEffect, useContext } from 'react'; import PropTypes from 'prop-types'; imp ...

Tips for accessing a component method in React from a WebSocket callback

Struggling with Javascript and React here. The goal is to update a component's state using push messages from a WebSocket. The issue lies in calling the function handlePushMessage from the WebSocket callback. Here's the code snippet: const Main ...

How can you easily ensure your React web app is responsive?

As I delve into building a web app using reactjs, one particular challenge I encounter is ensuring its responsiveness. An issue arises when the browser size changes, causing my components to overlap and lose alignment. Is there an easy solution to preven ...

Whenever the user hits the "Enter" key, a new element will be generated and added to the bottom of an existing element

I am in need of creating a new element at the end of another element. For instance, let's say I have this element: <div id="elmtobetrig">Element that should be followed by another element when the user hits enter after this text. <! ...

Editing the content of a div in real-time by dynamically updating the innerHTML

Looking for a way to dynamically change the innerHTML of a contentEditable div using pure JavaScript, without relying on the Rangy library. I want the change to occur on keyup without losing cursor position or focus. Here is an example setup: <div id=" ...

Notification window when the page is being loaded

My goal is to have a module or alert box display when my website is opened, and then disappear after 5 minutes. To see an example of what I'm looking for, take a look at this website: On that website, there is a facebook.com box that automatically d ...

The Google Closure Compiler Service is providing faulty code

I've been using Closure Compiler through to minify and obfuscate my JavaScript code, but I seem to be encountering an issue. To troubleshoot, I created a test page with a script. Here's the HTML5 code snippet: <!DOCTYPE html> <html> ...

Is it possible to utilize ANGULAR1 modules within ANGULAR2?

After completing numerous projects in AngularJS, I've noticed that the world is rapidly progressing towards HTML5 and the latest JavaScript engine technologies. The Angular team is now heavily focused on the development of Angular2, although there ar ...

What is the best way to keep the footer at the bottom and make it fixed in Material UI?

import * as React from "react"; import Container from "@mui/material/Container"; import Image from "next/image"; import Link from "@/src/Link"; import Box from "@mui/material/Box"; import Typography from &q ...

Delightful Bootstrap Tabs with Dynamic Content via Ajax

My website has a lot of tabs designed with Bootstrap. I wanted to make them responsive, so I tried using a plugin called Bootstrap Tabcollapse from https://github.com/flatlogic/bootstrap-tabcollapse (you can see a demo here: http://tabcollapse.okendoken.co ...

Problems during the installation of Webpack

Encountering Error Setting up Webpack and Babel with NPM npm ERR! Unexpected end of JSON input while parsing near '...pdragon":"^0.7.0","to' npm ERR! A complete log of this run can be found in: npm ERR! A complete log of this run can be found ...

Whenever I try to access my Node.js API from Heroku's live server, I encounter a CORS policy error

Whenever I try to access my Node.js API from the Angular app (running on a local setup) and host the API on Heroku's live server, I encounter the following error: CORS policy: No 'Access-Control-Allow-Origin'. Interestingly, when I host the ...

The dropdown menu is erroneously showing buttons instead of the intended options

My dropdown function is causing a small issue. The desired behavior is that if the selected value is "", labeled "Please Select" in the dropdown menu (I've added a comment in the function to pinpoint the problem), then buttons A, B, and C should not b ...

Creating a wrapper function for the map method in React

When attempting to incorporate a parameter into my map function in React, I encountered an issue where the return value turned null after encapsulating it within another function. This became apparent during debugging, especially when using console.log(mar ...

What is the best way to collect and store data from various sources in an HTML interface to a Google Spreadsheet?

Currently, I have a spreadsheet with a button that is supposed to link to a function in my Google Apps Script called openInputDialog. The goal is for this button to open an HTML UI where users can input text into five fields. This input should then be adde ...

Achieving a persistent footer at the bottom of the page within Material Angular's mat-sidenav-container while using the router-outlet

I am looking to keep my ngx-audio-player fixed at the bottom of the screen, similar to what you see on most music streaming websites. I currently have a structure with divs and various elements for dynamic content and playing music. The issue is that the ...

css styles for URLs that are sensitive to letter case

I'm currently at a loss for solutions as I can't seem to pinpoint the reason behind this peculiar behavior. I'm using Orchard CMS 1.4 along with a customized superfish stylesheet. Interestingly, the behavior varies depending on how the site ...

Navigating through a selection of imported images in a NextJS logo carousel using Vite

Currently, I am working on creating an automatic logo carousel in NextJS for my project. The development is being done in a next app on vite platform. After importing various images and assigning them values like Logo1, Logo2, etcetera, the goal is to have ...

Using parseFloat in JavaScript for German number formatting

Currently, I am working on incorporating a Vue.js component that is inspired by the example provided in this link: https://jsfiddle.net/mani04/bgzhw68m/. This is the structure of my source code: computed: { displayValue: { get ...