Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept CSS string in registerPreviewStyle #1162

Merged
merged 1 commit into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/Editor/EditorPreviewPane/EditorPreviewPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ export default class PreviewPane extends React.Component {
};

const styleEls = getPreviewStyles()
.map((style, i) => <link key={i} href={style} type="text/css" rel="stylesheet" />);
.map((style, i) => {
if (style.raw) {
return <style key={i}>{style.value}</style>
}
return <link key={i} href={style.value} type="text/css" rel="stylesheet" />;
});

if (!collection) {
return <Frame className="nc-previewPane-frame" head={styleEls} />;
Expand Down
7 changes: 5 additions & 2 deletions src/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ export default {

/**
* Preview Styles
*
* Valid options:
* - raw {boolean} if `true`, `style` value is expected to be a CSS string
*/
export function registerPreviewStyle(style) {
registry.previewStyles.push(style);
export function registerPreviewStyle(style, opts) {
registry.previewStyles.push({ ...opts, value: style });
};
export function getPreviewStyles() {
return registry.previewStyles;
Expand Down
15 changes: 15 additions & 0 deletions website/site/content/docs/beta-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ init({
// The registry works as expected, and can be used before or after init.
registry.registerPreviewTemplate(...);
```

## Raw CSS in `registerPreviewStyle`
`registerPreviewStyle` can now accept a CSS string, in addition to accepting a url. The feature is activated by passing in an object as the second argument, with `raw` set to a truthy value.This is critical for integrating with modern build tooling. Here's an example using webpack:

```js
/**
* Assumes a webpack project with `sass-loader` and `css-loader` installed.
* Takes advantage of the `toString` method in the return value of `css-loader`.
*/
import CMS from 'netlify-cms';
import styles from '!css-loader!sass-loader!../main.scss'

CMS.registerPreviewStyle(styles.toString(), { raw: true })
```