Skip to content

Commit 92e527a

Browse files
committed
feat: allow nonce on server style tag
1 parent 32fcc31 commit 92e527a

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

server/src/__snapshots__/index.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`<Style> add a nonce to the <style> tag 1`] = `"<style nonce=\\"abc\\" data-dash=\\"1abu38z\\" data-cache=\\"ui\\">.ui-1abu38z{display:block;}</style>"`;
4+
35
exports[`<Style> creates <style> tag based on html string 1`] = `"<style data-dash=\\"1abu38z\\" data-cache=\\"ui\\">.ui-1abu38z{display:block;}</style>"`;
46
57
exports[`<Style> creates <style> tag with cache key 1`] = `"<style data-dash=\\"1abu38z\\" data-cache=\\"abc\\">.abc-1abu38z{display:block;}</style>"`;

server/src/index.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,16 @@ describe("<Style>", () => {
5858

5959
expect(result).toMatchSnapshot();
6060
});
61+
62+
it("add a nonce to the <style> tag", () => {
63+
const style = styles.variants({
64+
default: "display: block;",
65+
});
66+
const Component = () => <div className={style()} />;
67+
const result = renderToStaticMarkup(
68+
<Style html={renderToStaticMarkup(<Component />)} nonce="abc" />
69+
);
70+
71+
expect(result).toMatchSnapshot();
72+
});
6173
});

server/src/index.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,23 @@ import * as React from "react";
1111
* @param html - The HTML generated by `renderToStaticMarkup()` or `renderToString()`
1212
* @param styles - An instance of `styles()`. Defaults to the default styles instance
1313
* in `@dash-ui/styles`.
14+
* @param options
15+
* @param options.nonce
1416
*/
1517
export function toComponent(
1618
html: string,
17-
styles: Styles<any, any> = defaultStyles
19+
styles: Styles<any, any> = defaultStyles,
20+
options: {
21+
nonce?: string;
22+
} = {}
1823
): React.ReactElement {
1924
const { dash } = styles;
2025
const { names, css } = createStylesFromString(html, styles);
2126

2227
return (
2328
<style
2429
key={dash.key}
25-
nonce={dash.sheet.nonce ? dash.sheet.nonce : void 0}
30+
nonce={options.nonce ?? (dash.sheet.nonce ? dash.sheet.nonce : void 0)}
2631
data-dash={names.join(" ")}
2732
data-cache={dash.key}
2833
dangerouslySetInnerHTML={{ __html: css }}
@@ -36,6 +41,7 @@ export function toComponent(
3641
* @param root0
3742
* @param root0.html
3843
* @param root0.styles
44+
* @param root0.nonce
3945
* @example
4046
* // _document.js
4147
* import React from 'react'
@@ -57,8 +63,8 @@ export function toComponent(
5763
* }
5864
* }
5965
*/
60-
export function Style({ html, styles }: StyleProps) {
61-
return toComponent(html, styles);
66+
export function Style({ html, styles, nonce }: StyleProps) {
67+
return toComponent(html, styles, { nonce });
6268
}
6369

6470
export interface StyleProps {
@@ -70,6 +76,10 @@ export interface StyleProps {
7076
* An instance of `styles()`. Defaults to the default styles instance in `@dash-ui/styles`.
7177
*/
7278
styles?: Styles<any, any>;
79+
/**
80+
* A nonce for the `<style>` tag.
81+
*/
82+
nonce?: Styles<any, any>["dash"]["sheet"]["nonce"];
7383
}
7484

7585
/**

0 commit comments

Comments
 (0)