{"id":13826715,"url":"https://github.com/bradlc/babel-plugin-tailwind-components","last_synced_at":"2025-04-04T13:11:44.453Z","repository":{"id":53712286,"uuid":"121886650","full_name":"bradlc/babel-plugin-tailwind-components","owner":"bradlc","description":"Use Tailwind with any CSS-in-JS library","archived":false,"fork":false,"pushed_at":"2023-01-03T23:15:29.000Z","size":448,"stargazers_count":333,"open_issues_count":36,"forks_count":23,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T08:05:43.669Z","etag":null,"topics":["babel","babel-plugin","babel-plugins-macros","css","css-in-js","tailwind","tailwindcss"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bradlc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-17T19:43:54.000Z","updated_at":"2025-01-30T00:57:52.000Z","dependencies_parsed_at":"2023-02-01T13:00:32.816Z","dependency_job_id":null,"html_url":"https://github.com/bradlc/babel-plugin-tailwind-components","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradlc%2Fbabel-plugin-tailwind-components","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradlc%2Fbabel-plugin-tailwind-components/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradlc%2Fbabel-plugin-tailwind-components/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradlc%2Fbabel-plugin-tailwind-components/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bradlc","download_url":"https://codeload.github.com/bradlc/babel-plugin-tailwind-components/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922241,"owners_count":20855345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["babel","babel-plugin","babel-plugins-macros","css","css-in-js","tailwind","tailwindcss"],"created_at":"2024-08-04T09:01:43.082Z","updated_at":"2025-04-04T13:11:44.424Z","avatar_url":"https://github.com/bradlc.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# babel-plugin-tailwind-components [![npm](https://img.shields.io/npm/v/babel-plugin-tailwind-components.svg)](https://www.npmjs.com/package/babel-plugin-tailwind-components) [![Babel Macro](https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat)](https://github.com/kentcdodds/babel-plugin-macros)\n\n\u003e Use [Tailwind](https://tailwindcss.com/) with any CSS-in-JS library\n\n## Prerequisites\n\nBefore you start using babel-plugin-tailwind-components you will need to ensure that you have a [Tailwind config file](https://tailwindcss.com/docs/configuration). You can grab the default config from the [Tailwind repo](https://github.com/tailwindcss/tailwindcss/blob/master/defaultConfig.stub.js).\n\nPlace the config file in your project root as `tailwind.js`. Alternatively you can specify a different filename in the [plugin options](#options).\n\n## Installation\n\nThere are two ways to use babel-plugin-tailwind-components. The recommended way is via [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros):\n\n```\nnpm install --save-dev babel-plugin-macros tailwind.macro\n```\n\n\u003e _Note: [tailwind.macro](https://github.com/bradlc/tailwind.macro) is merely an alias for [babel-plugin-tailwind-components/macro](https://github.com/bradlc/babel-plugin-tailwind-components/blob/master/src/macro.js)_\n\nThen add babel-plugin-macros to your babel config:\n\n```js\n{\n  \"plugins\": [\"macros\"]\n}\n```\n\n\u003e _Note: you will also need to install and enable `@babel/plugin-syntax-object-rest-spread` if you haven’t already_\n\nYou can now use Tailwind classes with your preferred CSS-in-JS library by importing `tailwind.macro`:\n\n```js\nimport styled from 'styled-components'\nimport tw from 'tailwind.macro'\n\nconst Button = styled('button')`\n  ${tw`font-mono text-sm text-red hover:text-blue`};\n`\n```\n\nAlternatively, you can use the plugin without babel-plugin-macros:\n\n```\nnpm install --save-dev babel-plugin-tailwind-components\n```\n\n```js\n// .babelrc\n{\n  \"plugins\": [\"tailwind-components\"]\n}\n```\n\nWhen using this method the `tw` tag is available anywhere without an explicit import:\n\n```js\nimport styled from 'styled-components'\n\nconst Button = styled('button')`\n  ${tw`font-mono text-sm text-red hover:text-blue`};\n`\n```\n\n## How it works\n\nThe babel plugin transforms the tagged template literal into a style object:\n\n**In**\n\n```js\nconst foo = tw`text-red hover:text-green sm:text-blue`\n```\n\n**Out**\n\n```js\nconst foo = {\n  color: '#e3342f',\n  ':hover': {\n    color: '#38c172'\n  },\n  '@media (min-width: 576px)': {\n    color: '#3490dc'\n  }\n}\n```\n\nThis style object format is compatible with most CSS-in-JS libraries, including [styled-components](#examples).\n\nSome libraries such as [styled-jsx](https://github.com/zeit/styled-jsx) do not support this format, so when used inside a `\u003cstyle\u003e` element the tagged template literal is transformed into a CSS string instead:\n\n**In**\n\n```js\n\u003cstyle jsx\u003e{`\n  .foo {\n    ${tw`text-red hover:text-green sm:text-blue`};\n  }\n`}\u003c/style\u003e\n```\n\n**Out**\n\n```js\n\u003cstyle jsx\u003e{`\n  .foo {\n    color: #e3342f;\n\n    \u0026:hover {\n      color: #38c172;\n    }\n\n    @media (min-width: 576px) {\n      color: #3490dc;\n    }\n  }\n`}\u003c/style\u003e\n```\n\n_Note: when using `hover:*`, `focus:*`, or media query (e.g. `sm:*`) class names the output is nested like above. You will need to use [styled-jsx-plugin-postcss](https://github.com/giuseppeg/styled-jsx-plugin-postcss) and [postcss-nested](https://github.com/postcss/postcss-nested) to get this to work._\n\n## Options\n\n`config`: path to your Tailwind config file. Defaults to `\"./tailwind.js\"`\n\n`format`: CSS output format. `\"object\"`, `\"string\"`, or `\"auto\"` (default) – `\"auto\"` will cause the output to be an object except when inside a `\u003cstyle\u003e` element. See [how it works](#how-it-works) for more info.\n\n```js\n// babel-plugin-macros.config.js\nmodule.exports = {\n  tailwind: {\n    config: './tailwind.js',\n    format: 'auto'\n  }\n}\n\n// or .babelrc\n{\n  \"plugins\": [\n    [\n      \"tailwind-components\", {\n        \"config\": \"./tailwind.js\",\n        \"format\": \"auto\"\n      }\n    ]\n  ]\n}\n```\n\n## Examples\n\n**[styled-components](https://github.com/styled-components/styled-components)**\n\n```js\nimport styled from 'styled-components'\nimport tw from 'tailwind.macro'\n\nconst Button = styled('button')`\n  ${tw`font-mono text-sm text-red hover:text-blue`};\n`\n```\n\n**[emotion](https://github.com/emotion-js/emotion)**\n\n```js\nimport styled from 'preact-emotion'\nimport { css } from 'emotion'\nimport tw from 'tailwind.macro'\n\nconst green = css(tw`text-green`)\n\nconst Button = styled('button')`\n  ${tw`font-mono text-sm text-red hover:text-blue`};\n`\n\nconst App = () =\u003e (\n  \u003cButton className={green} css={tw`uppercase`}\u003e\n    hello, world\n  \u003c/Button\u003e\n)\n```\n\n_Note: the `css` prop requires the use of [babel-plugin-emotion](https://github.com/emotion-js/emotion/tree/master/packages/babel-plugin-emotion)_\n\n**[glamor](https://github.com/threepointone/glamor)**\n\n```js\nimport { css } from 'glamor'\nimport tw from 'tailwind.macro'\n\nconst style = css(tw`font-mono text-sm text-red hover:text-blue`)\n\nconst App = () =\u003e \u003cdiv {...style}\u003ehello, world\u003c/div\u003e\n```\n\n**[styled-jsx](https://github.com/zeit/styled-jsx)**\n\n```js\nimport tw from 'tailwind.macro'\n\nconst App = () =\u003e (\n  \u003cdiv\u003e\n    \u003cdiv className=\"foo\"\u003ehello, world\u003c/div\u003e\n    \u003cstyle jsx\u003e{`\n      .foo {\n        ${tw`font-mono text-sm text-red hover:text-blue`};\n      }\n    `}\u003c/style\u003e\n  \u003c/div\u003e\n)\n```\n\n_Note: when using `hover:*`, `focus:*`, or media query (e.g. `sm:*`) class names the output is nested. You will need to use [styled-jsx-plugin-postcss](https://github.com/giuseppeg/styled-jsx-plugin-postcss) and [postcss-nested](https://github.com/postcss/postcss-nested) to get this to work._\n\n## Todo\n\n- ~~support for the [container class](https://tailwindcss.com/docs/container); [in progress](https://github.com/bradlc/babel-plugin-tailwind-components/pull/2)~~ container is now a plugin and there is no plan to support plugins\n- ~~support for multiple modifiers, e.g. `sm:hover:*`~~\n- ~~support for defaults; for example `rounded` should be an alias for `rounded-default`~~\n- add [CodeSandbox](https://codesandbox.io/) demos\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbradlc%2Fbabel-plugin-tailwind-components","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbradlc%2Fbabel-plugin-tailwind-components","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbradlc%2Fbabel-plugin-tailwind-components/lists"}