Gathering detailed insights and metrics for @mrcodes/twin.macro
Gathering detailed insights and metrics for @mrcodes/twin.macro
Gathering detailed insights and metrics for @mrcodes/twin.macro
Gathering detailed insights and metrics for @mrcodes/twin.macro
π¦ΉββοΈ Twin blends the magic of Tailwind with the flexibility of css-in-js (emotion, styled-components, stitches and goober) at build time.
npm install @mrcodes/twin.macro
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (99.36%)
TypeScript (0.64%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
952
Last Day
1
Last Week
2
Last Month
14
Last Year
79
MIT License
3 Stars
780 Commits
1 Forks
22 Branches
1 Contributors
Updated on Mar 04, 2023
Minified
Minified + Gzipped
Latest Version
2.8.4
Package Id
@mrcodes/twin.macro@2.8.4
Unpacked Size
384.53 kB
Size
77.48 kB
File Count
5
NPM Version
9.5.1
Node Version
18.16.0
Published on
Jun 20, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
0%
14
Compared to previous month
Last Year
-88.7%
79
Compared to previous year
15
1
34
Twin blends the magic of Tailwind with the flexibility of css-in-js
Demo twin on CodeSandbox β
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 tw, { styled, css } from 'twin.macro' 2- import tw from 'twin.macro' 3- import styled from '@emotion/styled' 4- import css from '@emotion/react'
πΉ 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
π Helpful suggestions for mistypings - Twin chimes in with class and variant values from your Tailwind config:
1β ml-7 was not found 2 3Try one of these classes: 4ml-0 [0] / ml-1 [0.25rem] / ml-2 [0.5rem] / ml-3 [0.75rem] / ml-4 [1rem] / ml-5 [1.25rem] / ml-6 [1.5rem] 5ml-8 [2rem] / ml-10 [2.5rem] / ml-12 [3rem] / ml-16 [4rem] / ml-20 [5rem] / ml-24 [6rem] / ml-32 [8rem] 6ml-40 [10rem] / ml-48 [12rem] / ml-56 [14rem] / ml-64 [16rem] / ml-auto [auto] / ml-px [1px]
π‘ Works with the official tailwind vscode plugin - Avoid having to look up your classes with auto-completions straight from your Tailwind config - See setup instructions β
π₯ Over 40 variants to prefix on your classes - The prefixes are βalways onβ and available for your classes
hocus:
to style hover + focus at the same timechecked:
, invalid:
and required:
sm:hover:first:bg-black
Check out the full list of variants β
π± 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))" />
π Add vanilla css that integrates with twins features
1const setCssVariables = () => <div tw="--base-color[#C0FFEE]" /> 2 3const customGridProperties = () => <div tw="grid-area[1 / 1 / 4 / 2]" /> 4 5const vendorPrefixes = () => <div tw="-webkit-mask-image[url(mask.png)]" />
ποΈ 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 β
π₯ 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 within many modern stacks - take a look at these examples to get started:
πΒ : Fresh example
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.
No security vulnerabilities found.