ruscus 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +356 -2
- package/dist/index.d.mts +1351 -0
- package/dist/index.mjs +661 -0
- package/dist/vite.d.mts +20 -0
- package/dist/vite.mjs +0 -0
- package/package.json +74 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Justin Locsei
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,357 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./assets/logo.svg" width="400" alt="Ruscus">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<br>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
Ruscus is a TypeScript library that provides a rich vocabulary for defining component styles, with child elements, variants, and nested and global styles as first-class concerns. It enables the reuse of styles through traits and shared media queries, and can be used with any view layer that allows you to apply class names to DOM elements.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
<!-- <toc> -->
|
|
14
|
+
- [Installation](#installation)
|
|
15
|
+
- [Getting Started](#getting-started)
|
|
16
|
+
- [Components](#components)
|
|
17
|
+
- [Elements](#elements)
|
|
18
|
+
- [Variants](#variants)
|
|
19
|
+
- [Nested Styles](#nested-styles)
|
|
20
|
+
- [Global Styles](#global-styles)
|
|
21
|
+
- [Identifiers](#identifiers)
|
|
22
|
+
- [Traits](#traits)
|
|
23
|
+
- [Queries](#queries)
|
|
24
|
+
- [Integration](#integration)
|
|
25
|
+
- [Vite](#vite)
|
|
26
|
+
- [Why the Name?](#why-the-name)
|
|
27
|
+
<!-- </toc> -->
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install ruscus
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Getting Started
|
|
36
|
+
|
|
37
|
+
Define component styles in a `card.ts` file:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { component } from 'ruscus';
|
|
41
|
+
|
|
42
|
+
export default component('card')
|
|
43
|
+
.css({
|
|
44
|
+
display: 'block',
|
|
45
|
+
padding: '1rem'
|
|
46
|
+
})
|
|
47
|
+
.elements(e => ({
|
|
48
|
+
title: e({
|
|
49
|
+
fontWeight: 'bold'
|
|
50
|
+
})
|
|
51
|
+
}))
|
|
52
|
+
.variants(s => ({
|
|
53
|
+
compact: s({ padding: '0.5rem' }),
|
|
54
|
+
featured: s({ border: '2px solid currentColor' })
|
|
55
|
+
}))
|
|
56
|
+
.apply();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Apply the styles to your component in a `card.tsx` file:
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
import C from './card.ts';
|
|
63
|
+
|
|
64
|
+
export function Card({ compact }: { compact: boolean }) {
|
|
65
|
+
return (
|
|
66
|
+
<article className={card.root(compact && 'compact')}>
|
|
67
|
+
<h1 className={C.title}>
|
|
68
|
+
A card
|
|
69
|
+
</h1>
|
|
70
|
+
</article>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The styles imported from `card.ts` are a map of element labels to class names that are present in the CSS generated through [bundler integration](#integration), allowing them to be used with any view layer.
|
|
76
|
+
|
|
77
|
+
## Components
|
|
78
|
+
|
|
79
|
+
All component styles begin with a call to the `component` function, which requires a unique ID. Ruscus uses a fluent API for defining component styles, allowing you to chain calls to definition methods before exposing the component's styles as class names via the `.apply` method.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { component } from 'ruscus';
|
|
83
|
+
|
|
84
|
+
export default component('button')
|
|
85
|
+
.css({ border: 0, cursor: 'pointer' })
|
|
86
|
+
.apply();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Component styles should be the default export of a `.ts` file. Style discovery happens via bundler integration that looks at the module graph, so there is no need for a particular file suffix. If you wish to use a convention like `.styles.ts`, however, you're more than welcome to.
|
|
90
|
+
|
|
91
|
+
## Elements
|
|
92
|
+
|
|
93
|
+
Components define child elements via the `.elements` method. Elements can themselves have child elements, allowing for deeply nested trees.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { component } from 'ruscus';
|
|
97
|
+
|
|
98
|
+
export default component('card')
|
|
99
|
+
.elements(e => ({
|
|
100
|
+
body: e({ padding: '1rem' }),
|
|
101
|
+
footer: e({ borderTop: '1px solid currentColor' }),
|
|
102
|
+
header: e().children(() => ({
|
|
103
|
+
icon: e({ marginRight: '0.5rem' }),
|
|
104
|
+
title: e({ fontWeight: '700' })
|
|
105
|
+
}))
|
|
106
|
+
}))
|
|
107
|
+
.apply();
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
A component's elements are known at compile time, allowing for safe access to each element's class name in the view layer:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import C from './card.ts';
|
|
114
|
+
|
|
115
|
+
// Valid
|
|
116
|
+
C.body
|
|
117
|
+
C.footer
|
|
118
|
+
C.header.root
|
|
119
|
+
C.header.icon
|
|
120
|
+
|
|
121
|
+
// Type error
|
|
122
|
+
C.missing
|
|
123
|
+
C.header.missing
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Variants
|
|
127
|
+
|
|
128
|
+
Variants can be defined on the component root or on any element:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { component } from 'ruscus';
|
|
132
|
+
|
|
133
|
+
export default component('card')
|
|
134
|
+
.elements(e => ({
|
|
135
|
+
body: e().variants(s => ({
|
|
136
|
+
active: s({ background: 'yellow' }),
|
|
137
|
+
compact: s({ padding: '0.5rem' })
|
|
138
|
+
}))
|
|
139
|
+
}))
|
|
140
|
+
.variants(s => ({
|
|
141
|
+
featured: s({ boxShadow: '0 0 0 2px currentColor' })
|
|
142
|
+
}))
|
|
143
|
+
.apply();
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Variants are applied in the view layer via functions on the element or component with variants. These functions take variant names and falsy or truthy values which can be used to conditionally apply the named variant:
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
import C from './card.ts';
|
|
150
|
+
|
|
151
|
+
export function Card({
|
|
152
|
+
active,
|
|
153
|
+
compact,
|
|
154
|
+
featured
|
|
155
|
+
}: {
|
|
156
|
+
active?: boolean;
|
|
157
|
+
compact?: boolean;
|
|
158
|
+
featured?: boolean;
|
|
159
|
+
}) {
|
|
160
|
+
return (
|
|
161
|
+
<article className={C.root(featured && 'featured')}>
|
|
162
|
+
<div className={C.body(active && 'active', compact && 'compact')}>
|
|
163
|
+
A card
|
|
164
|
+
</div>
|
|
165
|
+
</article>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Nested Styles
|
|
171
|
+
|
|
172
|
+
Use the `.nested` method for selectors that are relative to a component or element. A selector can use `&` to refer to the current node, or omit it to generate a selector whose parent is the class name of the component or element:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
import { component } from 'ruscus';
|
|
176
|
+
|
|
177
|
+
export default component('button')
|
|
178
|
+
.css({ color: 'black' })
|
|
179
|
+
.elements(e => ({
|
|
180
|
+
button: e({
|
|
181
|
+
color: 'red'
|
|
182
|
+
}).nested(s => ({
|
|
183
|
+
'&:active': s({ color: 'green' })
|
|
184
|
+
}))
|
|
185
|
+
}))
|
|
186
|
+
.nested((s, { els }) => ({
|
|
187
|
+
a: s({ color: 'blue' }),
|
|
188
|
+
[`&:hover ${els.button}`]: s({ textDecoration: 'underline' }),
|
|
189
|
+
}))
|
|
190
|
+
.apply();
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
The provider function can reference the component's variants, allowing styles to be applied to combinations of variants:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { component } from 'ruscus';
|
|
197
|
+
|
|
198
|
+
export default component('card')
|
|
199
|
+
.variants(s => ({
|
|
200
|
+
compact: s(),
|
|
201
|
+
featured: s()
|
|
202
|
+
}))
|
|
203
|
+
.nested((s, { variants }) => ({
|
|
204
|
+
[variants('compact', 'featured')]: s({
|
|
205
|
+
border: '2px solid currentColor'
|
|
206
|
+
})
|
|
207
|
+
}))
|
|
208
|
+
.apply();
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Global Styles
|
|
212
|
+
|
|
213
|
+
Use the `.global` method for selectors that are not scoped to the component root. This is useful for document-level rules and other intentionally shared CSS:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { component } from 'ruscus';
|
|
217
|
+
|
|
218
|
+
export default component('editor')
|
|
219
|
+
.global(s => ({
|
|
220
|
+
':root': s({ colorScheme: 'light dark' }),
|
|
221
|
+
html: s({ fontFamily: 'system-ui, sans-serif' })
|
|
222
|
+
}))
|
|
223
|
+
.apply();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Global definitions can also contain nested rules such as [media queries](#queries).
|
|
227
|
+
|
|
228
|
+
## Identifiers
|
|
229
|
+
|
|
230
|
+
Identifiers are component-specific values that are defined via the `.identifiers` method. String identifiers become class-scoped names, while variable identifiers become scoped CSS custom properties:
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
import { component } from 'ruscus';
|
|
234
|
+
|
|
235
|
+
export default component('card')
|
|
236
|
+
.identifiers({
|
|
237
|
+
animation: 'string',
|
|
238
|
+
spacing: 'variable'
|
|
239
|
+
})
|
|
240
|
+
.elements((e, { ids }) => ({
|
|
241
|
+
body: e({ animation: `${ids.animation} 500ms` }),
|
|
242
|
+
items: e({
|
|
243
|
+
display: 'flex',
|
|
244
|
+
gap: `var(${ids.spacing})`
|
|
245
|
+
})
|
|
246
|
+
}))
|
|
247
|
+
.global((s, { ids }) => ({
|
|
248
|
+
[`@keyframes ${ids.animation}`]: s.rules({
|
|
249
|
+
from: s({ opacity: 0 }),
|
|
250
|
+
to: s({ opacity: 1 })
|
|
251
|
+
})
|
|
252
|
+
}))
|
|
253
|
+
.apply();
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
To reference an identifier in the view layer, use the `identifiers` function:
|
|
257
|
+
|
|
258
|
+
```tsx
|
|
259
|
+
import C from './card.ts';
|
|
260
|
+
import { identifiers } from 'ruscus';
|
|
261
|
+
|
|
262
|
+
const ids = identifiers(C);
|
|
263
|
+
|
|
264
|
+
export function Card() {
|
|
265
|
+
return (
|
|
266
|
+
<article className={C.root} style={{ [ids.spacing]: '0.75rem' }}>
|
|
267
|
+
<div className={C.items}>
|
|
268
|
+
<div className={C.body}>A card</div>
|
|
269
|
+
</div>
|
|
270
|
+
</article>
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Traits
|
|
276
|
+
|
|
277
|
+
Traits provide a way to share styles across components and elements. They are defined once using the `trait` function, and can be applied to components and elements with the `.is` method:
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
import { component, trait } from 'ruscus';
|
|
281
|
+
|
|
282
|
+
const interactive = trait(s =>
|
|
283
|
+
s({ cursor: 'pointer' }).nested({
|
|
284
|
+
'&:hover': s({ opacity: '0.8' })
|
|
285
|
+
})
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
export default component('link')
|
|
289
|
+
.is(interactive)
|
|
290
|
+
.apply();
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Queries
|
|
294
|
+
|
|
295
|
+
The `queries` function maps internal labels to CSS at-rule queries:
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
import { queries } from 'ruscus';
|
|
299
|
+
|
|
300
|
+
export const responsive = queries({
|
|
301
|
+
narrow: '@container card (min-width: 400px)',
|
|
302
|
+
wide: '@media (min-width: 768px)',
|
|
303
|
+
});
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
To use queries to style a component, apply them in a `.global` block:
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
import { component } from 'ruscus';
|
|
310
|
+
import { responsive } from './helpers.ts';
|
|
311
|
+
|
|
312
|
+
export default component('card')
|
|
313
|
+
.elements(e => ({
|
|
314
|
+
body: e(),
|
|
315
|
+
title: e()
|
|
316
|
+
}))
|
|
317
|
+
.global((s, { els }) => responsive({
|
|
318
|
+
narrow: {
|
|
319
|
+
[els.title]: s({ fontSize: '0.8em' }),
|
|
320
|
+
},
|
|
321
|
+
wide: {
|
|
322
|
+
[els.body]: s({ padding: '2rem' })
|
|
323
|
+
}
|
|
324
|
+
}))
|
|
325
|
+
.apply();
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Each key from the initial `queries` call can be provided to the applicator function. Queries may be omitted, and any unknown queries will result in a type error.
|
|
329
|
+
|
|
330
|
+
## Integration
|
|
331
|
+
|
|
332
|
+
Ruscus provides a framework for defining component styles and structure without taking a stance on how those styles are applied to your app. Integrations connect the generated CSS to a build tool or framework.
|
|
333
|
+
|
|
334
|
+
### Vite
|
|
335
|
+
|
|
336
|
+
Install Vite alongside Ruscus:
|
|
337
|
+
|
|
338
|
+
```sh
|
|
339
|
+
npm install vite
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Add the Ruscus plugin to serve CSS during development and include it in production builds to your Vite config:
|
|
343
|
+
|
|
344
|
+
```ts
|
|
345
|
+
import { defineConfig } from 'vite';
|
|
346
|
+
import ruscus from 'ruscus/vite';
|
|
347
|
+
|
|
348
|
+
export default defineConfig({
|
|
349
|
+
plugins: [ruscus()]
|
|
350
|
+
});
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
All style files that define a default export of an applied component and are included in the module graph for your entry point will be used to generate component CSS.
|
|
354
|
+
|
|
355
|
+
## Why the Name?
|
|
356
|
+
|
|
357
|
+
In [floristry](https://www.instagram.com/justinlocsei/), ruscus is a common foliage that provides structure without calling too much attention to itself.
|