Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@types/styled-components
Advanced tools
The @types/styled-components package provides TypeScript type definitions for the styled-components library, which is a popular CSS-in-JS library. These type definitions allow TypeScript developers to use styled-components in a type-safe manner, ensuring that styles are applied correctly and that component props are managed according to their expected types.
Type-safe styled components
This feature allows developers to create styled components with TypeScript, providing type safety for props. The example shows a button component with a 'primary' prop that changes the background color based on its value.
import styled from 'styled-components';
interface ButtonProps {
primary: boolean;
}
const Button = styled.button<ButtonProps>`
background-color: ${props => props.primary ? 'blue' : 'gray'};
color: white;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
`;
Extending styles with TypeScript
This feature demonstrates how to extend an existing styled component while adding additional props with type definitions. The FancyButton extends BasicButton and includes a 'large' prop to adjust the font size.
import styled from 'styled-components';
const BasicButton = styled.button`
color: white;
background-color: black;
padding: 5px 10px;
`;
interface FancyButtonProps {
large: boolean;
}
const FancyButton = styled(BasicButton)<FancyButtonProps>`
font-size: ${props => props.large ? '20px' : '12px'};
border: 2px solid white;
`;
Similar to styled-components, @emotion/styled also allows for defining styles directly in JavaScript or TypeScript. It offers a similar API but focuses more on performance optimizations and has a slightly different syntax for defining styles with the css prop.
JSS (JavaScript Style Sheets) is another CSS-in-JS library that allows for similar styling capabilities. While it provides a different API than styled-components, it supports plugins and dynamic styling, making it a flexible alternative.
npm install --save @types/styled-components
This package contains type definitions for styled-components (https://github.com/styled-components/styled-components).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/styled-components.
These definitions were written by Igor Oleinikov, Ihor Chulinda, Jessica Franco, Jason Killian, Sebastian Silbermann, Matthew Wagerfield, Yuki Ito, and Aaron Reisman.
FAQs
TypeScript definitions for styled-components
The npm package @types/styled-components receives a total of 1,678,003 weekly downloads. As such, @types/styled-components popularity was classified as popular.
We found that @types/styled-components demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.