Twin.macro 使用教程
1. 项目介绍
Twin.macro 是一个开源项目,旨在将 Tailwind CSS 的魔力与 CSS-in-JS 的灵活性结合在一起。它支持多种 CSS-in-JS 库,如 Emotion、Styled-components、Solid-styled-components、Stitches 和 Goober。Twin.macro 在构建时将 Tailwind 类转换为 CSS 对象,从而避免了在客户端运行时增加额外的代码。
2. 项目快速启动
安装
首先,你需要安装 Twin.macro 和相关的 CSS-in-JS 库。以下是使用 Styled-components 的示例:
npm install twin.macro styled-components
配置 Babel
在你的项目根目录下创建一个 .babelrc 文件,并添加以下配置:
{
"plugins": ["babel-plugin-macros"]
}
使用 Twin.macro
在你的 React 组件中使用 Twin.macro:
import tw from 'twin.macro';
const Button = tw.button`
bg-blue-500
hover:bg-blue-700
text-white
font-bold
py-2
px-4
rounded
`;
const App = () => (
<div>
<Button>Click me</Button>
</div>
);
export default App;
3. 应用案例和最佳实践
使用 Variant Groups
Twin.macro 支持在多个类上同时应用变体。例如:
import 'twin.macro';
const interactionStyles = () => (
<div tw="hover:(text-black underline) focus:(text-blue-500 underline)" />
);
const mediaStyles = () => <div tw="sm:(w-4 mt-3) lg:(w-8 mt-6)" />;
使用 Theme 导入
你可以使用 theme 导入来添加 Tailwind 配置中的值:
import { css, theme } from 'twin.macro';
const Input = () => (
<input css={[css({ color: theme`colors.purple.500` })]} />
);
4. 典型生态项目
1. Emotion
Emotion 是一个流行的 CSS-in-JS 库,Twin.macro 与之兼容,提供了强大的样式功能。
2. Styled-components
Styled-components 是另一个广泛使用的 CSS-in-JS 库,Twin.macro 可以与其无缝集成,提供类似的功能。
3. Solid-styled-components
Solid-styled-components 是 SolidJS 的样式库,Twin.macro 也支持与其集成,为 SolidJS 开发者提供 Tailwind CSS 的便利。
4. Stitches
Stitches 是一个现代的 CSS-in-JS 库,Twin.macro 可以与其结合使用,提供高效的样式解决方案。
5. Goober
Goober 是一个轻量级的 CSS-in-JS 库,Twin.macro 可以与其集成,提供简洁的样式管理。
通过这些生态项目的支持,Twin.macro 为开发者提供了灵活且强大的样式解决方案。
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0153- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
LongCat-Video-Avatar-1.5最新开源LongCat-Video-Avatar 1.5 版本,这是一款经过升级的开源框架,专注于音频驱动人物视频生成的极致实证优化与生产级就绪能力。该版本在 LongCat-Video 基础模型之上构建,可生成高度稳定的商用级虚拟人视频,支持音频-文本转视频(AT2V)、音频-文本-图像转视频(ATI2V)以及视频续播等原生任务,并能无缝兼容单流与多流音频输入。00
auto-devAutoDev 是一个 AI 驱动的辅助编程插件。AutoDev 支持一键生成测试、代码、提交信息等,还能够与您的需求管理系统(例如Jira、Trello、Github Issue 等)直接对接。 在IDE 中,您只需简单点击,AutoDev 会根据您的需求自动为您生成代码。Kotlin03
Intern-S2-PreviewIntern-S2-Preview,这是一款高效的350亿参数科学多模态基础模型。除了常规的参数与数据规模扩展外,Intern-S2-Preview探索了任务扩展:通过提升科学任务的难度、多样性与覆盖范围,进一步释放模型能力。Python00
skillhubopenJiuwen 生态的 Skill 托管与分发开源方案,支持自建与可选 ClawHub 兼容。Python0112