Skip to content

Commit 634a0b7

Browse files
author
John Richard Chipps-Harding
authored
Transpiler CLI tool (#12)
* Transcoder * more * default variant support * CLI * Tidy * lots * Docs * more docs * Other docs * typo * refactor * fix typo * version bump
1 parent 490db69 commit 634a0b7

File tree

7 files changed

+657
-88
lines changed

7 files changed

+657
-88
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ lib
77
!src/*
88
!test/*
99
.idea
10-
.DS_Store
10+
.DS_Store
11+
test/cli/styles.ts

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,27 @@ const Link = styled("a", {
195195
});
196196
```
197197

198+
### Other Components
199+
200+
You can also style other components from other ecosystems. As long as the component has a `className` prop, styling should propagate.
201+
202+
Example extending the standard Next.js `Link` component:
203+
204+
```tsx
205+
import { styled } from "@phntms/css-components";
206+
import NextLink from "next/link";
207+
import css from "./styles.module.css";
208+
209+
const Link = styled(NextLink, {
210+
css: css.link,
211+
variants: {
212+
big: {
213+
true: css.big,
214+
},
215+
},
216+
});
217+
```
218+
198219
### Type Helper
199220

200221
We have included a helper that allows you to access the types of the variants you have defined.
@@ -213,6 +234,70 @@ const Button = styled("button", {
213234
type ButtonTypes = CSSComponentPropType<typeof Button, "primary">;
214235
```
215236

237+
## CLI Tool (Experimental)
238+
239+
We have included a CLI tool that allows you to generate components from CSS files which confirm to a specific naming convention. This is highly experimental and is subject to change.
240+
241+
Consider this CSS file:
242+
243+
```css
244+
nav.topBar {
245+
background-color: #aaa;
246+
padding: 32px;
247+
}
248+
249+
nav.topBar_fixed_true {
250+
position: fixed;
251+
bottom: 0;
252+
left: 0;
253+
right: 0;
254+
}
255+
```
256+
257+
You can generate a component from this file with the following command:
258+
259+
```bash
260+
npx @phntms/css-components --css styles.module.css
261+
262+
# or if you have the package installed
263+
npm exec css-components --css styles.module.css
264+
```
265+
266+
This will output a file called `styles.ts` that looks like this:
267+
268+
```ts
269+
import { styled } from "@phntms/css-components";
270+
271+
import css from "./test.css";
272+
273+
export const TopBar = styled("nav", {
274+
css: css.topBar,
275+
variants: {
276+
fixed: {
277+
true: css.topBar_fixed_true,
278+
},
279+
},
280+
});
281+
```
282+
283+
### Possible CSS definitions:
284+
285+
- `a.link` Allowing you to define a base style for the component. This means it will be an anchor tag with the css class `link`.
286+
- `a.link_big_true` Lets you set the styling for a variant called `big` with the value `true`.
287+
- `a.link_theme_light_default` Same as above but also sets the variant as the default value.
288+
- `a.link_big_true_theme_light` Gives you the ability to define compound variants styles.
289+
290+
### CLI Options
291+
292+
- `--css` The path to the CSS file you want to generate a component from. This can also be a recursive glob pattern allowing you to scan your entire components directory.
293+
- `--output` The filename for the output file. Defaults to `styles.ts` which will be saved in the same directory as the CSS file.
294+
295+
Example to generate components from all CSS files in the components directory:
296+
297+
```bash
298+
npx @phntms/css-components --css ./src/components/**/*.css --output styles.ts
299+
```
300+
216301
[npm-image]: https://img.shields.io/npm/v/@phntms/css-components.svg?style=flat-square&logo=react
217302
[npm-url]: https://npmjs.org/package/@phntms/css-components
218303
[npm-downloads-image]: https://img.shields.io/npm/dm/@phntms/css-components.svg

0 commit comments

Comments
 (0)