Gathering detailed insights and metrics for twin.macro
Gathering detailed insights and metrics for twin.macro
Gathering detailed insights and metrics for twin.macro
Gathering detailed insights and metrics for twin.macro
🦹♂️ Twin blends the magic of Tailwind with the flexibility of css-in-js (emotion, styled-components, solid-styled-components, stitches and goober) at build time.
npm install twin.macro
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
7,951 Stars
1,068 Commits
184 Forks
32 Watching
6 Branches
46 Contributors
Updated on 27 Nov 2024
TypeScript (94.73%)
JavaScript (5.27%)
Cumulative downloads
Total Downloads
Last day
2.9%
12,044
Compared to previous day
Last week
6%
68,348
Compared to previous week
Last month
6.6%
295,819
Compared to previous month
Last year
3.2%
3,773,694
Compared to previous year
1
52
The magic of Tailwind with the flexibility of css-in-js.
Style jsx elements using Tailwind classes:
1import 'twin.macro' 2 3const Input = () => <input tw="border hover:border-black" />
Nest Twin’s tw
import within a css prop to add conditional styles:
1import tw from 'twin.macro' 2 3const Input = ({ hasHover }) => ( 4 <input css={[tw`border`, hasHover && tw`hover:border-black`]} /> 5)
Or mix sass styles with the css import:
1import tw, { css } from 'twin.macro' 2 3const hoverStyles = css` 4 &:hover { 5 border-color: black; 6 ${tw`text-black`} 7 } 8` 9const Input = ({ hasHover }) => ( 10 <input css={[tw`border`, hasHover && hoverStyles]} /> 11)
You can also use the tw import to create and style new components:
1import tw from 'twin.macro' 2 3const Input = tw.input`border hover:border-black`
And clone and style existing components:
1const PurpleInput = tw(Input)`border-purple-500`
Switch to the styled import to add conditional styling:
1import tw, { styled } from 'twin.macro' 2 3const StyledInput = styled.input(({ hasBorder }) => [ 4 `color: black;`, 5 hasBorder && tw`border-purple-500`, 6]) 7const Input = () => <StyledInput hasBorder />
Or use backticks to mix with sass styles:
1import tw, { styled } from 'twin.macro' 2 3const StyledInput = styled.input` 4 color: black; 5 ${({ hasBorder }) => hasBorder && tw`border-purple-500`} 6` 7const Input = () => <StyledInput hasBorder />
When babel runs over your javascript or typescript files at compile time, twin grabs your classes and converts them into css objects. These css objects are then passed into your chosen css-in-js library without the need for an extra client-side bundle:
1import tw from 'twin.macro' 2 3tw`text-sm md:text-lg` 4 5// ↓ ↓ ↓ ↓ ↓ ↓ 6 7{ 8 fontSize: '0.875rem', 9 '@media (min-width: 768px)': { 10 fontSize: '1.125rem', 11 }, 12}
👌 Simple imports - Twin collapses imports from common styling libraries into a single import:
1- import styled from '@emotion/styled' 2- import css from '@emotion/react' 3+ import { styled, css } from 'twin.macro'
🐹 Adds no size to your build - Twin converts the classes you’ve used into css objects using Babel and then compiles away, leaving no runtime code
🍱 Apply variants to multiple classes at once with variant groups
1import 'twin.macro' 2 3const interactionStyles = () => ( 4 <div tw="hover:(text-black underline) focus:(text-blue-500 underline)" /> 5) 6 7const mediaStyles = () => <div tw="sm:(w-4 mt-3) lg:(w-8 mt-6)" /> 8 9const pseudoElementStyles = () => <div tw="before:(block w-10 h-10 bg-black)" /> 10 11const stackedVariants = () => <div tw="sm:hover:(bg-black text-white)" /> 12 13const groupsInGroups = () => <div tw="sm:(bg-black hover:(bg-white w-10))" />
🛎 Helpful suggestions for mistypings - Twin chimes in with class and variant values from your Tailwind config:
1✕ ml-1.25 was not found 2 3Try one of these classes: 4 5- ml-1.5 > 0.375rem 6- ml-1 > 0.25rem 7- ml-10 > 2.5rem
🖌️ Use the theme import to add values from your tailwind config
1import { css, theme } from 'twin.macro' 2 3const Input = () => <input css={css({ color: theme`colors.purple.500` })} />
See more examples using the theme import →
💡 Works with the official tailwind vscode plugin - Avoid having to look up your classes with auto-completions straight from your Tailwind config - setup instructions →
💥 Add !important to any class with a trailing or leading bang!
1<div tw="hidden!" /> || <div tw="!hidden" /> 2// ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 3<div css={{ "display": "none !important" }} />
Add !important to multiple classes with bracket groups:
1<div tw="(hidden ml-auto)!" /> 2// ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ 3<div css={{ "display": "none !important", "marginLeft": "auto !important" }} />
Twin works with many modern stacks - take a look at these examples to get started:
Drop into our Discord server for announcements, help and styling chat.
This project stemmed from babel-plugin-tailwind-components so a big shout out goes to Brad Cornes for the amazing work he produced. Styling with tailwind.macro has been such a pleasure.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
10 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More