Skip to content

Commit

Permalink
Enable embedded CSS formatting on save
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Dec 6, 2024
1 parent fb14616 commit 7ee034b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .changeset/ninety-books-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
'playroom': minor
---

Enable embedded CSS formatting on save

CSS authored inside `style` tags inside a playroom will now be formatted as CSS when wrapped in a `css` template literal tag.

A `css` template literal tag can be injected into your playroom scope via the [custom scope] feature:

```js
// customScope.js

export default () => {
return {
css: (css) => css,
};
};
```

This template literal tag can then be used in your playroom:

```jsx
<style>
{css`
.foo {
color: red;
}
`}
</style>
<div className="foo">Hello, world!</div>
```

[custom scope]: https://github.com/seek-oss/playroom?tab=readme-ov-file#custom-scope
1 change: 1 addition & 0 deletions cypress/projects/basic/useScope.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default () => ({
hello: () => 'HELLO',
world: () => 'WORLD',
css: (css) => css,
});
3 changes: 2 additions & 1 deletion src/utils/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import prettier from 'prettier/standalone';
import babel from 'prettier/parser-babel';
import postcss from 'prettier/parser-postcss';
import type { CursorPosition } from '../StoreContext/StoreContext';
import { insertAtCursor } from './cursor';

Expand All @@ -21,7 +22,7 @@ export const runPrettier = ({
return prettier.formatWithCursor(code, {
cursorOffset,
parser: 'babel',
plugins: [babel],
plugins: [babel, postcss],
});
} catch (e) {
// Just a formatting error so we pass
Expand Down

0 comments on commit 7ee034b

Please sign in to comment.