Skip to content

Commit cd08bb2

Browse files
committed
Added css string template method.
1 parent 558d56b commit cd08bb2

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,27 @@ import { html } from 'onlybuild';
224224
export default html`<h1>Hello, world!</h1>`;
225225
```
226226

227-
Install the [lit-html](https://marketplace.visualstudio.com/items?itemName=bierner.lit-html) plugin in VS Code to help format the HTML on save.
227+
Install the [lit-html](https://marketplace.visualstudio.com/items?itemName=bierner.lit-html) and [prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) plugin in VS Code to help format the HTML on save.
228+
229+
### <code>&#96;css&#96;</code> String Template Utility
230+
231+
The `onlybuild` library includes an optional <code>&#96;css&#96;</code> string template utility that can be used to add syntax highlighting and formatting to CSS, making it easier to author CSS in JavaScript.
232+
233+
```javascript
234+
import { css } from 'onlybuild';
235+
236+
const styles = css`
237+
body {
238+
color: red;
239+
}
240+
`;
241+
242+
export default html`<style>
243+
${styles}
244+
</style>`;
245+
```
246+
247+
Install the [prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) plugin in VS Code to help format the CSS on save.
228248

229249
## File Structure
230250

src/css.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* node:coverage disable */
2+
3+
import test, { describe } from 'node:test';
4+
import assert from 'node:assert';
5+
6+
import { css } from './css.js';
7+
8+
describe('css string template utility', async () => {
9+
test('simple css string', () => {
10+
assert.equal(
11+
css`
12+
body {
13+
color: red;
14+
}
15+
`,
16+
`
17+
body {
18+
color: red;
19+
}
20+
`
21+
);
22+
});
23+
test('simple css string with variable', () => {
24+
const color = 'red';
25+
assert.equal(
26+
css`
27+
body {
28+
color: ${color};
29+
}
30+
`,
31+
`
32+
body {
33+
color: red;
34+
}
35+
`
36+
);
37+
});
38+
});

src/css.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* String template utility that adds syntax highlighting and formatting in text editors.
3+
*
4+
* @param {TemplateStringsArray} strings
5+
* @param {any[]} values
6+
*/
7+
8+
export const css = (strings: TemplateStringsArray, ...values: any[]) => {
9+
const processedValues = values.map(value =>
10+
Array.isArray(value) ? value.join('') : value
11+
);
12+
13+
return strings.reduce(
14+
(prev, curr, i) => `${prev}${curr}${processedValues[i] || ''}`,
15+
''
16+
);
17+
};

src/html.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ describe('html string template utility', async () => {
1212
'<span><b>this is a test</b></span>'
1313
);
1414
});
15+
test('simple html string with variable', () => {
16+
const name = 'test';
17+
assert.equal(
18+
html`<span><b>this is a ${name}</b></span>`,
19+
'<span><b>this is a test</b></span>'
20+
);
21+
});
1522
test('map', () => {
1623
assert.equal(
1724
html`<ul>

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { css } from './css.js';
12
export { html } from './html.js';

0 commit comments

Comments
 (0)