oncss 1.2.12 → 1.2.13
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/core.cjs +51 -28
- package/core.cjs.map +1 -1
- package/core.js +51 -28
- package/core.js.map +1 -1
- package/index.cjs.map +1 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +397 -397
- package/types.d.ts +3 -2
package/readme.md
CHANGED
|
@@ -1,397 +1,397 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<img width="120" src="https://raw.githubusercontent.com/devnax/oncss/main/logo.png" alt="ONCSS Logo">
|
|
3
|
-
</p>
|
|
4
|
-
|
|
5
|
-
<h1 align="center">ONCSS</h1>
|
|
6
|
-
|
|
7
|
-
`oncss` is a CSS-in-JS library that provides developers with a powerful `css` function to style their web applications. It enables modern styling techniques, including nested selectors, responsive design, and dynamic keyframes, all while offering seamless integration with JavaScript frameworks like React.
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## Installation
|
|
12
|
-
|
|
13
|
-
Install the `oncss` package via npm:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
npm install oncss
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Import the `css` function in your project:
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
import css from 'oncss';
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
---
|
|
26
|
-
|
|
27
|
-
## Core Concept: The `css` Function
|
|
28
|
-
|
|
29
|
-
The `css` function is the heart of `oncss`, designed to dynamically generate and inject CSS into your application. It supports:
|
|
30
|
-
|
|
31
|
-
- **CSS Properties**: Use standard CSS properties and values.
|
|
32
|
-
- **Nested Selectors**: Apply styles to child elements or states using `&`.
|
|
33
|
-
- **Media Queries**: Implement responsive designs with `@media` rules.
|
|
34
|
-
- **Keyframes**: Create animations with `@keyframes`.
|
|
35
|
-
- **Global Styles**: Apply styles globally with `@global`.
|
|
36
|
-
- **Custom Breakpoints**: Define reusable breakpoints for responsiveness.
|
|
37
|
-
- **Other At-Rules**: Utilize additional at-rules like `@container`, `@layer`, and `@supports`.
|
|
38
|
-
|
|
39
|
-
### Basic Example
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
const buttonStyles = css({
|
|
43
|
-
backgroundColor: 'blue',
|
|
44
|
-
color: 'white',
|
|
45
|
-
padding: '10px 20px',
|
|
46
|
-
borderRadius: '5px',
|
|
47
|
-
'&:hover': {
|
|
48
|
-
backgroundColor: 'darkblue',
|
|
49
|
-
},
|
|
50
|
-
'@media (min-width: 768px)': {
|
|
51
|
-
padding: '15px 30px',
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
console.log(buttonStyles);
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
## Configuration Options
|
|
61
|
-
|
|
62
|
-
The `css` function can be customized through an options object:
|
|
63
|
-
|
|
64
|
-
### Available Properties
|
|
65
|
-
|
|
66
|
-
| Property | Type | Description |
|
|
67
|
-
| ------------- | ---------- | ------------------------------------------------------------------------------------------------- |
|
|
68
|
-
| `classPrefix` | `string` | Adds a prefix to generated class names. |
|
|
69
|
-
| `breakpoints` | `object` | Custom breakpoints for responsive designs. |
|
|
70
|
-
| `aliases` | `object` | Custom shorthand properties for CSS rules. |
|
|
71
|
-
| `injectStyle` | `boolean` | Controls whether styles are auto-injected. |
|
|
72
|
-
| `skipProps` | `function` | Filters out unwanted properties. Receives `prop`, `value`, and `dept` as arguments. |
|
|
73
|
-
| `getValue` | `function` | Transforms property values dynamically. Receives `value`, `prop`, `css`, and `dept` as arguments. |
|
|
74
|
-
| `getProps` | `function` | Customizes specific property handling. Receives `prop`, `value`, `css`, and `dept` as arguments. |
|
|
75
|
-
|
|
76
|
-
### Example with Options
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
const styles = css({
|
|
80
|
-
fontSize: 16,
|
|
81
|
-
padding: 10,
|
|
82
|
-
}, {
|
|
83
|
-
classPrefix: 'myprefix',
|
|
84
|
-
breakpoints: {
|
|
85
|
-
sm: 480,
|
|
86
|
-
md: 768,
|
|
87
|
-
lg: 1024,
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Using Breakpoints
|
|
93
|
-
|
|
94
|
-
You can use the defined breakpoints in your styles to create responsive designs:
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
const responsiveStyles = css({
|
|
98
|
-
fontSize: 14,
|
|
99
|
-
padding: {
|
|
100
|
-
sm: 12,
|
|
101
|
-
lg: 24
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
}, {
|
|
105
|
-
breakpoints: {
|
|
106
|
-
sm: 480,
|
|
107
|
-
md: 768,
|
|
108
|
-
lg: 1024,
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
---
|
|
114
|
-
|
|
115
|
-
## React Integration
|
|
116
|
-
|
|
117
|
-
oncss integrates seamlessly with React. Simply pass the generated class name to your component.
|
|
118
|
-
|
|
119
|
-
### React Example
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
import React from 'react';
|
|
123
|
-
import css from 'oncss';
|
|
124
|
-
|
|
125
|
-
const buttonStyle = css({
|
|
126
|
-
backgroundColor: 'green',
|
|
127
|
-
color: 'white',
|
|
128
|
-
padding: '10px 20px',
|
|
129
|
-
borderRadius: '8px',
|
|
130
|
-
'&:hover': {
|
|
131
|
-
backgroundColor: 'darkgreen',
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
function Button() {
|
|
136
|
-
return <button className={buttonStyle.toString()}>Click Me</button>;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export default Button;
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
---
|
|
143
|
-
|
|
144
|
-
## Advanced Features
|
|
145
|
-
|
|
146
|
-
### Nested Selectors
|
|
147
|
-
|
|
148
|
-
Apply styles to child elements or pseudo-classes:
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
const cardStyles = css({
|
|
152
|
-
padding: '20px',
|
|
153
|
-
border: '1px solid #ccc',
|
|
154
|
-
'& h1': {
|
|
155
|
-
fontSize: '24px',
|
|
156
|
-
margin: 0,
|
|
157
|
-
},
|
|
158
|
-
'&:hover': {
|
|
159
|
-
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Media Queries
|
|
165
|
-
|
|
166
|
-
Easily add responsive styles:
|
|
167
|
-
|
|
168
|
-
```typescript
|
|
169
|
-
const responsiveStyles = css({
|
|
170
|
-
fontSize: '14px',
|
|
171
|
-
'@media (min-width: 768px)': {
|
|
172
|
-
fontSize: '18px',
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
### Keyframes
|
|
178
|
-
|
|
179
|
-
Define and use animations:
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
const animationStyles = css({
|
|
183
|
-
'@keyframes fadeIn': {
|
|
184
|
-
from: { opacity: 0 },
|
|
185
|
-
to: { opacity: 1 },
|
|
186
|
-
},
|
|
187
|
-
animation: 'fadeIn 2s ease-in-out',
|
|
188
|
-
});
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### Global Styles
|
|
192
|
-
|
|
193
|
-
Apply global styles effortlessly:
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
const globalStyles = css({
|
|
197
|
-
'@global': {
|
|
198
|
-
body: {
|
|
199
|
-
margin: 0,
|
|
200
|
-
fontFamily: 'Arial, sans-serif',
|
|
201
|
-
},
|
|
202
|
-
a: {
|
|
203
|
-
color: 'blue',
|
|
204
|
-
textDecoration: 'none',
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
---
|
|
211
|
-
|
|
212
|
-
## Supported At-Rules
|
|
213
|
-
|
|
214
|
-
`oncss` supports various CSS at-rules to enhance your styling capabilities. Here is a list of supported at-rules with descriptions:
|
|
215
|
-
|
|
216
|
-
| At-Rule | Description |
|
|
217
|
-
| ------------ | ---------------------------------------------------------------------------- |
|
|
218
|
-
| `@media` | Used for applying styles based on media queries for responsive design. |
|
|
219
|
-
| `@keyframes` | Defines animations that can be applied to elements. |
|
|
220
|
-
| `@global` | Applies styles globally across the entire application. |
|
|
221
|
-
| `@container` | Used for container queries to apply styles based on container size. |
|
|
222
|
-
| `@layer` | Defines style layers to control the order of style application. |
|
|
223
|
-
| `@supports` | Applies styles based on the support of specific CSS features in the browser. |
|
|
224
|
-
|
|
225
|
-
---
|
|
226
|
-
|
|
227
|
-
## Server-Side Styling
|
|
228
|
-
|
|
229
|
-
`oncss` supports server-side rendering (SSR) by utilizing the `CSSFactory` to store and manage generated CSS styles. This allows you to extract and inject styles into your server-rendered HTML.
|
|
230
|
-
|
|
231
|
-
### Example with React
|
|
232
|
-
|
|
233
|
-
Here's an example of how to use `oncss` for server-side rendering with React:
|
|
234
|
-
|
|
235
|
-
```tsx
|
|
236
|
-
import React from 'react';
|
|
237
|
-
import ReactDOMServer from 'react-dom/server';
|
|
238
|
-
import css, { CSSFactory } from 'oncss';
|
|
239
|
-
|
|
240
|
-
const buttonStyle = css({
|
|
241
|
-
backgroundColor: 'blue',
|
|
242
|
-
color: 'white',
|
|
243
|
-
padding: '10px 20px',
|
|
244
|
-
borderRadius: '5px',
|
|
245
|
-
'&:hover': {
|
|
246
|
-
backgroundColor: 'darkblue',
|
|
247
|
-
},
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
function Button() {
|
|
251
|
-
return <button className={buttonStyle}>Click Me</button>;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const App = () => (
|
|
255
|
-
<div>
|
|
256
|
-
<Button />
|
|
257
|
-
</div>
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
// Render the component to a string
|
|
261
|
-
const html = ReactDOMServer.renderToString(<App />);
|
|
262
|
-
|
|
263
|
-
let styles: any = Array.from(CSSFactory.values()).map((style) => {
|
|
264
|
-
return `<style data-href="${style.classname}">${style.css}</style>`
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
// Inject the styles into the HTML
|
|
268
|
-
const fullHtml = `
|
|
269
|
-
<!DOCTYPE html>
|
|
270
|
-
<html lang="en">
|
|
271
|
-
<head>
|
|
272
|
-
<meta charset="UTF-8">
|
|
273
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
274
|
-
<title>SSR with oncss</title>
|
|
275
|
-
${styles}
|
|
276
|
-
</head>
|
|
277
|
-
<body>
|
|
278
|
-
<div id="root">${html}</div>
|
|
279
|
-
</body>
|
|
280
|
-
</html>
|
|
281
|
-
`;
|
|
282
|
-
|
|
283
|
-
console.log(fullHtml);
|
|
284
|
-
```
|
|
285
|
-
|
|
286
|
-
In this example, the `CSSFactory` is used to collect all generated CSS styles during the server-side rendering process. These styles are then injected into the HTML document, ensuring that the styles are applied correctly when the page is loaded in the browser.
|
|
287
|
-
|
|
288
|
-
---
|
|
289
|
-
|
|
290
|
-
## Utility Functions
|
|
291
|
-
|
|
292
|
-
### CSSFactory
|
|
293
|
-
|
|
294
|
-
`CSSFactory` is a global cache for storing generated CSS styles. It helps in reusing styles and avoiding redundant style generation.
|
|
295
|
-
|
|
296
|
-
### formatCSSProp
|
|
297
|
-
|
|
298
|
-
`formatCSSProp` is a utility function that converts camelCase CSS property names to kebab-case.
|
|
299
|
-
|
|
300
|
-
```typescript
|
|
301
|
-
import { formatCSSProp } from 'oncss';
|
|
302
|
-
|
|
303
|
-
const formattedProp = formatCSSProp('backgroundColor');
|
|
304
|
-
console.log(formattedProp); // 'background-color'
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
### formatCSSValue
|
|
308
|
-
|
|
309
|
-
`formatCSSValue` is a utility function that formats CSS values, adding units like `px` where necessary.
|
|
310
|
-
|
|
311
|
-
```typescript
|
|
312
|
-
import { formatCSSValue } from 'oncss';
|
|
313
|
-
|
|
314
|
-
const formattedValue = formatCSSValue('width', 100);
|
|
315
|
-
console.log(formattedValue); // '100px'
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
---
|
|
319
|
-
|
|
320
|
-
## TypeScript Integration
|
|
321
|
-
|
|
322
|
-
`oncss` provides full TypeScript support, allowing you to define types for your CSS properties and options.
|
|
323
|
-
|
|
324
|
-
### Defining CSS Properties
|
|
325
|
-
|
|
326
|
-
You can define the types for your CSS properties using the `CSSProps` type:
|
|
327
|
-
|
|
328
|
-
```typescript
|
|
329
|
-
import { CSSProps } from 'oncss';
|
|
330
|
-
|
|
331
|
-
interface MyAliases {
|
|
332
|
-
customColor?: string;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const styles: CSSProps<MyAliases, 'sm' | 'md' | 'lg'> = {
|
|
336
|
-
backgroundColor: 'blue',
|
|
337
|
-
customColor: 'red',
|
|
338
|
-
'@media (min-width: 768px)': {
|
|
339
|
-
backgroundColor: 'green',
|
|
340
|
-
},
|
|
341
|
-
};
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
### Using Options with Types
|
|
345
|
-
|
|
346
|
-
You can also define types for the options object:
|
|
347
|
-
|
|
348
|
-
```typescript
|
|
349
|
-
import { CSSOptionProps } from 'oncss';
|
|
350
|
-
|
|
351
|
-
const options: CSSOptionProps<MyAliases, 'sm' | 'md' | 'lg'> = {
|
|
352
|
-
classPrefix: 'myprefix',
|
|
353
|
-
breakpoints: {
|
|
354
|
-
sm: 480,
|
|
355
|
-
md: 768,
|
|
356
|
-
lg: 1024,
|
|
357
|
-
},
|
|
358
|
-
aliases: {
|
|
359
|
-
customColor: (value) => ({ color: value }),
|
|
360
|
-
},
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
const styles = css({
|
|
364
|
-
fontSize: 16,
|
|
365
|
-
padding: 10,
|
|
366
|
-
}, options);
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
---
|
|
370
|
-
|
|
371
|
-
## Conclusion
|
|
372
|
-
|
|
373
|
-
`oncss` simplifies styling for modern web applications. Its robust feature set, from responsive design to keyframe animations, makes it an invaluable tool for developers.
|
|
374
|
-
|
|
375
|
-
## Author
|
|
376
|
-
|
|
377
|
-
<table>
|
|
378
|
-
<tr>
|
|
379
|
-
<td>
|
|
380
|
-
<img src="https://raw.githubusercontent.com/devnax/devnax/main/me-circle-200.png" alt="devnax" width="100" height="100">
|
|
381
|
-
</td>
|
|
382
|
-
<td>
|
|
383
|
-
<strong>Naxrul Ahmed</strong><br>
|
|
384
|
-
<a href="https://github.com/devnax">GitHub Profile</a><br>
|
|
385
|
-
<a href="https://www.npmjs.com/~devnax">npm Profile</a><br>
|
|
386
|
-
<a href="https://github.com/devnax/open-source">Open Source Projects</a>
|
|
387
|
-
</td>
|
|
388
|
-
</tr>
|
|
389
|
-
</table>
|
|
390
|
-
|
|
391
|
-
<h2>⚡️ Where to find me</h2>
|
|
392
|
-
|
|
393
|
-
<p><a target="_blank" href="mailto:devnaxrul@gmail.com" style="display: inline-block;"><img src="https://img.shields.io/badge/-Email-05122A?style=for-the-badge&logo=gmail&logoColor=white&color=orange" alt="gmail" /></a>
|
|
394
|
-
<a target="_blank" href="https://twitter.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/twitter-x?style=for-the-badge&logo=x&logoColor=white&color=%230f1419" alt="twitter" /></a>
|
|
395
|
-
<a target="_blank" href="https://www.linkedin.com/in/devnax" style="display: inline-block;"><img src="https://img.shields.io/badge/linkedin-logo?style=for-the-badge&logo=linkedin&logoColor=white&color=%230a77b6" alt="linkedin" /></a>
|
|
396
|
-
<a target="_blank" href="https://www.facebook.com/devnax" style="display: inline-block;"><img src="https://img.shields.io/badge/facebook-logo?style=for-the-badge&logo=facebook&logoColor=white&color=%230866ff" alt="facebook" /></a>
|
|
397
|
-
<a target="_blank" href="https://www.instagram.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/instagram-logo?style=for-the-badge&logo=instagram&logoColor=white&color=%23F35369" alt="instagram" /></a></p>
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="120" src="https://raw.githubusercontent.com/devnax/oncss/main/logo.png" alt="ONCSS Logo">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">ONCSS</h1>
|
|
6
|
+
|
|
7
|
+
`oncss` is a CSS-in-JS library that provides developers with a powerful `css` function to style their web applications. It enables modern styling techniques, including nested selectors, responsive design, and dynamic keyframes, all while offering seamless integration with JavaScript frameworks like React.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install the `oncss` package via npm:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install oncss
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Import the `css` function in your project:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import css from 'oncss';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Core Concept: The `css` Function
|
|
28
|
+
|
|
29
|
+
The `css` function is the heart of `oncss`, designed to dynamically generate and inject CSS into your application. It supports:
|
|
30
|
+
|
|
31
|
+
- **CSS Properties**: Use standard CSS properties and values.
|
|
32
|
+
- **Nested Selectors**: Apply styles to child elements or states using `&`.
|
|
33
|
+
- **Media Queries**: Implement responsive designs with `@media` rules.
|
|
34
|
+
- **Keyframes**: Create animations with `@keyframes`.
|
|
35
|
+
- **Global Styles**: Apply styles globally with `@global`.
|
|
36
|
+
- **Custom Breakpoints**: Define reusable breakpoints for responsiveness.
|
|
37
|
+
- **Other At-Rules**: Utilize additional at-rules like `@container`, `@layer`, and `@supports`.
|
|
38
|
+
|
|
39
|
+
### Basic Example
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const buttonStyles = css({
|
|
43
|
+
backgroundColor: 'blue',
|
|
44
|
+
color: 'white',
|
|
45
|
+
padding: '10px 20px',
|
|
46
|
+
borderRadius: '5px',
|
|
47
|
+
'&:hover': {
|
|
48
|
+
backgroundColor: 'darkblue',
|
|
49
|
+
},
|
|
50
|
+
'@media (min-width: 768px)': {
|
|
51
|
+
padding: '15px 30px',
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
console.log(buttonStyles);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Configuration Options
|
|
61
|
+
|
|
62
|
+
The `css` function can be customized through an options object:
|
|
63
|
+
|
|
64
|
+
### Available Properties
|
|
65
|
+
|
|
66
|
+
| Property | Type | Description |
|
|
67
|
+
| ------------- | ---------- | ------------------------------------------------------------------------------------------------- |
|
|
68
|
+
| `classPrefix` | `string` | Adds a prefix to generated class names. |
|
|
69
|
+
| `breakpoints` | `object` | Custom breakpoints for responsive designs. |
|
|
70
|
+
| `aliases` | `object` | Custom shorthand properties for CSS rules. |
|
|
71
|
+
| `injectStyle` | `boolean` | Controls whether styles are auto-injected. |
|
|
72
|
+
| `skipProps` | `function` | Filters out unwanted properties. Receives `prop`, `value`, and `dept` as arguments. |
|
|
73
|
+
| `getValue` | `function` | Transforms property values dynamically. Receives `value`, `prop`, `css`, and `dept` as arguments. |
|
|
74
|
+
| `getProps` | `function` | Customizes specific property handling. Receives `prop`, `value`, `css`, and `dept` as arguments. |
|
|
75
|
+
|
|
76
|
+
### Example with Options
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const styles = css({
|
|
80
|
+
fontSize: 16,
|
|
81
|
+
padding: 10,
|
|
82
|
+
}, {
|
|
83
|
+
classPrefix: 'myprefix',
|
|
84
|
+
breakpoints: {
|
|
85
|
+
sm: 480,
|
|
86
|
+
md: 768,
|
|
87
|
+
lg: 1024,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Using Breakpoints
|
|
93
|
+
|
|
94
|
+
You can use the defined breakpoints in your styles to create responsive designs:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const responsiveStyles = css({
|
|
98
|
+
fontSize: 14,
|
|
99
|
+
padding: {
|
|
100
|
+
sm: 12,
|
|
101
|
+
lg: 24
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
}, {
|
|
105
|
+
breakpoints: {
|
|
106
|
+
sm: 480,
|
|
107
|
+
md: 768,
|
|
108
|
+
lg: 1024,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## React Integration
|
|
116
|
+
|
|
117
|
+
oncss integrates seamlessly with React. Simply pass the generated class name to your component.
|
|
118
|
+
|
|
119
|
+
### React Example
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import React from 'react';
|
|
123
|
+
import css from 'oncss';
|
|
124
|
+
|
|
125
|
+
const buttonStyle = css({
|
|
126
|
+
backgroundColor: 'green',
|
|
127
|
+
color: 'white',
|
|
128
|
+
padding: '10px 20px',
|
|
129
|
+
borderRadius: '8px',
|
|
130
|
+
'&:hover': {
|
|
131
|
+
backgroundColor: 'darkgreen',
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
function Button() {
|
|
136
|
+
return <button className={buttonStyle.toString()}>Click Me</button>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default Button;
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Advanced Features
|
|
145
|
+
|
|
146
|
+
### Nested Selectors
|
|
147
|
+
|
|
148
|
+
Apply styles to child elements or pseudo-classes:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
const cardStyles = css({
|
|
152
|
+
padding: '20px',
|
|
153
|
+
border: '1px solid #ccc',
|
|
154
|
+
'& h1': {
|
|
155
|
+
fontSize: '24px',
|
|
156
|
+
margin: 0,
|
|
157
|
+
},
|
|
158
|
+
'&:hover': {
|
|
159
|
+
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.1)',
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Media Queries
|
|
165
|
+
|
|
166
|
+
Easily add responsive styles:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
const responsiveStyles = css({
|
|
170
|
+
fontSize: '14px',
|
|
171
|
+
'@media (min-width: 768px)': {
|
|
172
|
+
fontSize: '18px',
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Keyframes
|
|
178
|
+
|
|
179
|
+
Define and use animations:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const animationStyles = css({
|
|
183
|
+
'@keyframes fadeIn': {
|
|
184
|
+
from: { opacity: 0 },
|
|
185
|
+
to: { opacity: 1 },
|
|
186
|
+
},
|
|
187
|
+
animation: 'fadeIn 2s ease-in-out',
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Global Styles
|
|
192
|
+
|
|
193
|
+
Apply global styles effortlessly:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const globalStyles = css({
|
|
197
|
+
'@global': {
|
|
198
|
+
body: {
|
|
199
|
+
margin: 0,
|
|
200
|
+
fontFamily: 'Arial, sans-serif',
|
|
201
|
+
},
|
|
202
|
+
a: {
|
|
203
|
+
color: 'blue',
|
|
204
|
+
textDecoration: 'none',
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Supported At-Rules
|
|
213
|
+
|
|
214
|
+
`oncss` supports various CSS at-rules to enhance your styling capabilities. Here is a list of supported at-rules with descriptions:
|
|
215
|
+
|
|
216
|
+
| At-Rule | Description |
|
|
217
|
+
| ------------ | ---------------------------------------------------------------------------- |
|
|
218
|
+
| `@media` | Used for applying styles based on media queries for responsive design. |
|
|
219
|
+
| `@keyframes` | Defines animations that can be applied to elements. |
|
|
220
|
+
| `@global` | Applies styles globally across the entire application. |
|
|
221
|
+
| `@container` | Used for container queries to apply styles based on container size. |
|
|
222
|
+
| `@layer` | Defines style layers to control the order of style application. |
|
|
223
|
+
| `@supports` | Applies styles based on the support of specific CSS features in the browser. |
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Server-Side Styling
|
|
228
|
+
|
|
229
|
+
`oncss` supports server-side rendering (SSR) by utilizing the `CSSFactory` to store and manage generated CSS styles. This allows you to extract and inject styles into your server-rendered HTML.
|
|
230
|
+
|
|
231
|
+
### Example with React
|
|
232
|
+
|
|
233
|
+
Here's an example of how to use `oncss` for server-side rendering with React:
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
import React from 'react';
|
|
237
|
+
import ReactDOMServer from 'react-dom/server';
|
|
238
|
+
import css, { CSSFactory } from 'oncss';
|
|
239
|
+
|
|
240
|
+
const buttonStyle = css({
|
|
241
|
+
backgroundColor: 'blue',
|
|
242
|
+
color: 'white',
|
|
243
|
+
padding: '10px 20px',
|
|
244
|
+
borderRadius: '5px',
|
|
245
|
+
'&:hover': {
|
|
246
|
+
backgroundColor: 'darkblue',
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
function Button() {
|
|
251
|
+
return <button className={buttonStyle}>Click Me</button>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const App = () => (
|
|
255
|
+
<div>
|
|
256
|
+
<Button />
|
|
257
|
+
</div>
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
// Render the component to a string
|
|
261
|
+
const html = ReactDOMServer.renderToString(<App />);
|
|
262
|
+
|
|
263
|
+
let styles: any = Array.from(CSSFactory.values()).map((style) => {
|
|
264
|
+
return `<style data-href="${style.classname}">${style.css}</style>`
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
// Inject the styles into the HTML
|
|
268
|
+
const fullHtml = `
|
|
269
|
+
<!DOCTYPE html>
|
|
270
|
+
<html lang="en">
|
|
271
|
+
<head>
|
|
272
|
+
<meta charset="UTF-8">
|
|
273
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
274
|
+
<title>SSR with oncss</title>
|
|
275
|
+
${styles}
|
|
276
|
+
</head>
|
|
277
|
+
<body>
|
|
278
|
+
<div id="root">${html}</div>
|
|
279
|
+
</body>
|
|
280
|
+
</html>
|
|
281
|
+
`;
|
|
282
|
+
|
|
283
|
+
console.log(fullHtml);
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
In this example, the `CSSFactory` is used to collect all generated CSS styles during the server-side rendering process. These styles are then injected into the HTML document, ensuring that the styles are applied correctly when the page is loaded in the browser.
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## Utility Functions
|
|
291
|
+
|
|
292
|
+
### CSSFactory
|
|
293
|
+
|
|
294
|
+
`CSSFactory` is a global cache for storing generated CSS styles. It helps in reusing styles and avoiding redundant style generation.
|
|
295
|
+
|
|
296
|
+
### formatCSSProp
|
|
297
|
+
|
|
298
|
+
`formatCSSProp` is a utility function that converts camelCase CSS property names to kebab-case.
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { formatCSSProp } from 'oncss';
|
|
302
|
+
|
|
303
|
+
const formattedProp = formatCSSProp('backgroundColor');
|
|
304
|
+
console.log(formattedProp); // 'background-color'
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### formatCSSValue
|
|
308
|
+
|
|
309
|
+
`formatCSSValue` is a utility function that formats CSS values, adding units like `px` where necessary.
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import { formatCSSValue } from 'oncss';
|
|
313
|
+
|
|
314
|
+
const formattedValue = formatCSSValue('width', 100);
|
|
315
|
+
console.log(formattedValue); // '100px'
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
## TypeScript Integration
|
|
321
|
+
|
|
322
|
+
`oncss` provides full TypeScript support, allowing you to define types for your CSS properties and options.
|
|
323
|
+
|
|
324
|
+
### Defining CSS Properties
|
|
325
|
+
|
|
326
|
+
You can define the types for your CSS properties using the `CSSProps` type:
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
import { CSSProps } from 'oncss';
|
|
330
|
+
|
|
331
|
+
interface MyAliases {
|
|
332
|
+
customColor?: string;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const styles: CSSProps<MyAliases, 'sm' | 'md' | 'lg'> = {
|
|
336
|
+
backgroundColor: 'blue',
|
|
337
|
+
customColor: 'red',
|
|
338
|
+
'@media (min-width: 768px)': {
|
|
339
|
+
backgroundColor: 'green',
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### Using Options with Types
|
|
345
|
+
|
|
346
|
+
You can also define types for the options object:
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
import { CSSOptionProps } from 'oncss';
|
|
350
|
+
|
|
351
|
+
const options: CSSOptionProps<MyAliases, 'sm' | 'md' | 'lg'> = {
|
|
352
|
+
classPrefix: 'myprefix',
|
|
353
|
+
breakpoints: {
|
|
354
|
+
sm: 480,
|
|
355
|
+
md: 768,
|
|
356
|
+
lg: 1024,
|
|
357
|
+
},
|
|
358
|
+
aliases: {
|
|
359
|
+
customColor: (value) => ({ color: value }),
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const styles = css({
|
|
364
|
+
fontSize: 16,
|
|
365
|
+
padding: 10,
|
|
366
|
+
}, options);
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## Conclusion
|
|
372
|
+
|
|
373
|
+
`oncss` simplifies styling for modern web applications. Its robust feature set, from responsive design to keyframe animations, makes it an invaluable tool for developers.
|
|
374
|
+
|
|
375
|
+
## Author
|
|
376
|
+
|
|
377
|
+
<table>
|
|
378
|
+
<tr>
|
|
379
|
+
<td>
|
|
380
|
+
<img src="https://raw.githubusercontent.com/devnax/devnax/main/me-circle-200.png" alt="devnax" width="100" height="100">
|
|
381
|
+
</td>
|
|
382
|
+
<td>
|
|
383
|
+
<strong>Naxrul Ahmed</strong><br>
|
|
384
|
+
<a href="https://github.com/devnax">GitHub Profile</a><br>
|
|
385
|
+
<a href="https://www.npmjs.com/~devnax">npm Profile</a><br>
|
|
386
|
+
<a href="https://github.com/devnax/open-source">Open Source Projects</a>
|
|
387
|
+
</td>
|
|
388
|
+
</tr>
|
|
389
|
+
</table>
|
|
390
|
+
|
|
391
|
+
<h2>⚡️ Where to find me</h2>
|
|
392
|
+
|
|
393
|
+
<p><a target="_blank" href="mailto:devnaxrul@gmail.com" style="display: inline-block;"><img src="https://img.shields.io/badge/-Email-05122A?style=for-the-badge&logo=gmail&logoColor=white&color=orange" alt="gmail" /></a>
|
|
394
|
+
<a target="_blank" href="https://twitter.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/twitter-x?style=for-the-badge&logo=x&logoColor=white&color=%230f1419" alt="twitter" /></a>
|
|
395
|
+
<a target="_blank" href="https://www.linkedin.com/in/devnax" style="display: inline-block;"><img src="https://img.shields.io/badge/linkedin-logo?style=for-the-badge&logo=linkedin&logoColor=white&color=%230a77b6" alt="linkedin" /></a>
|
|
396
|
+
<a target="_blank" href="https://www.facebook.com/devnax" style="display: inline-block;"><img src="https://img.shields.io/badge/facebook-logo?style=for-the-badge&logo=facebook&logoColor=white&color=%230866ff" alt="facebook" /></a>
|
|
397
|
+
<a target="_blank" href="https://www.instagram.com/devnaxx" style="display: inline-block;"><img src="https://img.shields.io/badge/instagram-logo?style=for-the-badge&logo=instagram&logoColor=white&color=%23F35369" alt="instagram" /></a></p>
|