|
| 1 | +# wc-css-transform |
| 2 | + |
| 3 | +Transform Web Components CSS to regular CSS format with data attribute mapping. |
| 4 | + |
| 5 | +A powerful tool for converting web component CSS (using `:host` selectors) into regular CSS compatible formats, with automatic data attribute mapping for better framework integration. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- 🎯 **Transform `:host` selectors** to regular CSS format |
| 10 | +- 🔄 **Data attribute mapping** - automatically convert attributes to `data-*` format |
| 11 | +- 🛠️ **PostCSS plugin** - integrate with your existing build pipeline |
| 12 | +- 📦 **Framework agnostic** - works with React, Vue, Angular, and more |
| 13 | +- 🎨 **CSS compatible** - outputs clean CSS that works with styled-components, emotion, etc. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install wc-css-transform |
| 19 | +# or |
| 20 | +pnpm add wc-css-transform |
| 21 | +# or |
| 22 | +yarn add wc-css-transform |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Basic Usage |
| 28 | + |
| 29 | +```typescript |
| 30 | +import { wcCssTransform } from 'wc-css-transform'; |
| 31 | + |
| 32 | +const input = ` |
| 33 | +:host { |
| 34 | + color: red; |
| 35 | + font-size: 16px; |
| 36 | +} |
| 37 | +
|
| 38 | +:host([active]) { |
| 39 | + background: blue; |
| 40 | +} |
| 41 | +
|
| 42 | +:host(:hover) { |
| 43 | + opacity: 0.8; |
| 44 | +} |
| 45 | +`; |
| 46 | + |
| 47 | +const output = wcCssTransform(input); |
| 48 | + |
| 49 | +console.log(output); |
| 50 | +// Output: |
| 51 | +// color: red; |
| 52 | +// font-size: 16px; |
| 53 | +// |
| 54 | +// &[active] { |
| 55 | +// background: blue; |
| 56 | +// } |
| 57 | +// |
| 58 | +// &:hover { |
| 59 | +// opacity: 0.8; |
| 60 | +// } |
| 61 | +``` |
| 62 | + |
| 63 | +### PostCSS Plugin |
| 64 | + |
| 65 | +```javascript |
| 66 | +import postcss from 'postcss'; |
| 67 | +import { postcssPlugin } from 'wc-css-transform'; |
| 68 | + |
| 69 | +const result = await postcss([ |
| 70 | + postcssPlugin |
| 71 | +]).process(input); |
| 72 | +``` |
| 73 | + |
| 74 | +## API |
| 75 | + |
| 76 | +### `wcCssTransform(input, options)` |
| 77 | + |
| 78 | +Transform web component CSS to regular CSS format. |
| 79 | + |
| 80 | +**Parameters:** |
| 81 | +- `input` (string): CSS input string |
| 82 | +- `options` (object): Configuration options |
| 83 | + |
| 84 | +**Options:** |
| 85 | +- `hostTag` (string): Tag to replace `:host` with (default: `"&"`) |
| 86 | +- `dataAttributes` (boolean): Enable data attribute mapping (default: `false`) |
| 87 | +- `excludeFromData` (string[]): Attributes to exclude from data- prefix |
| 88 | +- `layer` (string | false): CSS layer name (default: `false`) |
| 89 | + |
| 90 | +### `postcssPlugin(options)` |
| 91 | + |
| 92 | +PostCSS plugin for build pipeline integration. |
| 93 | + |
| 94 | +## Examples |
| 95 | + |
| 96 | +### React with styled-components |
| 97 | + |
| 98 | +```typescript |
| 99 | +import styled from 'styled-components'; |
| 100 | +import { wcCssTransform } from 'wc-css-transform'; |
| 101 | + |
| 102 | +const webComponentCSS = ` |
| 103 | +:host { |
| 104 | + color: var(--primary-color); |
| 105 | +} |
| 106 | +
|
| 107 | +:host([size="large"]) { |
| 108 | + font-size: 24px; |
| 109 | +} |
| 110 | +`; |
| 111 | + |
| 112 | +const StyledComponent = styled.div` |
| 113 | + ${wcCssTransform(webComponentCSS, { hostTag: '&', dataAttributes: true })} |
| 114 | +`; |
| 115 | +``` |
| 116 | + |
| 117 | +### Vue with CSS modules |
| 118 | + |
| 119 | +```typescript |
| 120 | +import { wcCssTransform } from 'wc-css-transform'; |
| 121 | + |
| 122 | +const componentCSS = wcCssTransform(` |
| 123 | +:host { |
| 124 | + display: flex; |
| 125 | + align-items: center; |
| 126 | +} |
| 127 | +
|
| 128 | +:host([disabled]) { |
| 129 | + opacity: 0.5; |
| 130 | + pointer-events: none; |
| 131 | +} |
| 132 | +`, { hostTag: '&', dataAttributes: true }); |
| 133 | +``` |
| 134 | + |
| 135 | +## Configuration |
| 136 | + |
| 137 | +### Data Attribute Mapping |
| 138 | + |
| 139 | +When `dataAttributes: true`, attributes are automatically prefixed with `data-`: |
| 140 | + |
| 141 | +```css |
| 142 | +/* Input */ |
| 143 | +:host([id]) { } |
| 144 | +:host([size="large"]) { } |
| 145 | +[active] { } |
| 146 | + |
| 147 | +/* Output */ |
| 148 | +&[data-id] { } |
| 149 | +&[data-size="large"] { } |
| 150 | +[data-active] { } |
| 151 | +``` |
| 152 | + |
| 153 | +### Exclude Attributes |
| 154 | + |
| 155 | +Use `excludeFromData` to prevent certain attributes from being prefixed: |
| 156 | + |
| 157 | +```typescript |
| 158 | +wcCssTransform(input, { |
| 159 | + dataAttributes: true, |
| 160 | + excludeFromData: ['part', 'class', 'id'] |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +## Build Integration |
| 165 | + |
| 166 | +### Vite |
| 167 | + |
| 168 | +```javascript |
| 169 | +// vite.config.js |
| 170 | +import { defineConfig } from 'vite'; |
| 171 | +import { postcssPlugin } from 'wc-css-transform'; |
| 172 | + |
| 173 | +export default defineConfig({ |
| 174 | + css: { |
| 175 | + postcss: { |
| 176 | + plugins: [ |
| 177 | + postcssPlugin({ |
| 178 | + dataAttributes: true |
| 179 | + }) |
| 180 | + ] |
| 181 | + } |
| 182 | + } |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +## License |
| 187 | + |
| 188 | +MIT |
| 189 | + |
| 190 | +## Contributing |
| 191 | + |
| 192 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 193 | + |
0 commit comments