Skip to content

Commit

Permalink
Merge pull request #25 from neogeek/feature/css
Browse files Browse the repository at this point in the history
[feat] Added css string template method.
  • Loading branch information
neogeek committed May 21, 2024
2 parents 558d56b + cd08bb2 commit 9b77ac7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,27 @@ import { html } from 'onlybuild';
export default html`<h1>Hello, world!</h1>`;
```

Install the [lit-html](https://marketplace.visualstudio.com/items?itemName=bierner.lit-html) plugin in VS Code to help format the HTML on save.
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.

### <code>&#96;css&#96;</code> String Template Utility

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.

```javascript
import { css } from 'onlybuild';

const styles = css`
body {
color: red;
}
`;

export default html`<style>
${styles}
</style>`;
```

Install the [prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) plugin in VS Code to help format the CSS on save.

## File Structure

Expand Down
38 changes: 38 additions & 0 deletions src/css.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* node:coverage disable */

import test, { describe } from 'node:test';
import assert from 'node:assert';

import { css } from './css.js';

describe('css string template utility', async () => {
test('simple css string', () => {
assert.equal(
css`
body {
color: red;
}
`,
`
body {
color: red;
}
`
);
});
test('simple css string with variable', () => {
const color = 'red';
assert.equal(
css`
body {
color: ${color};
}
`,
`
body {
color: red;
}
`
);
});
});
17 changes: 17 additions & 0 deletions src/css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* String template utility that adds syntax highlighting and formatting in text editors.
*
* @param {TemplateStringsArray} strings
* @param {any[]} values
*/

export const css = (strings: TemplateStringsArray, ...values: any[]) => {
const processedValues = values.map(value =>
Array.isArray(value) ? value.join('') : value
);

return strings.reduce(
(prev, curr, i) => `${prev}${curr}${processedValues[i] || ''}`,
''
);
};
7 changes: 7 additions & 0 deletions src/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ describe('html string template utility', async () => {
'<span><b>this is a test</b></span>'
);
});
test('simple html string with variable', () => {
const name = 'test';
assert.equal(
html`<span><b>this is a ${name}</b></span>`,
'<span><b>this is a test</b></span>'
);
});
test('map', () => {
assert.equal(
html`<ul>
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { css } from './css.js';
export { html } from './html.js';

0 comments on commit 9b77ac7

Please sign in to comment.