Skip to content

Commit eb0fcee

Browse files
committed
Added a new strictCSP mode that avoids all inline styles and uses CSS vars for styling
1 parent f339d71 commit eb0fcee

File tree

7 files changed

+321
-46
lines changed

7 files changed

+321
-46
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,42 @@ const App = () => {
6767
};
6868
```
6969

70+
## Content Security Policy (CSP)
71+
72+
react-hot-toast supports strict Content Security Policies through an opt-in strict CSP mode.
73+
74+
### Default Mode
75+
76+
By default, react-hot-toast uses inline styles for maximum flexibility. This requires `style-src 'unsafe-inline'` in your CSP.
77+
78+
### Strict CSP Mode
79+
80+
For applications with strict CSP that disallow inline styles, enable strict CSP mode:
81+
82+
```jsx
83+
import toast, { Toaster } from 'react-hot-toast';
84+
85+
<Toaster strictCSP={true} />
86+
```
87+
88+
In strict CSP mode:
89+
- All inline `style` props are ignored
90+
- Styling must be done via CSS classes and CSS variables
91+
- Toast positioning uses CSS flexbox instead of inline transforms
92+
- Fully compatible with CSP `style-src 'nonce-...'` directives
93+
94+
The library uses [goober](https://github.com/cristianbote/goober) for styling. To support CSP nonces, set `window.__nonce__` before your app loads:
95+
96+
```html
97+
<script nonce="your-nonce-here">
98+
window.__nonce__ = 'your-nonce-here';
99+
</script>
100+
```
101+
102+
goober will automatically apply the nonce to its generated `<style>` elements.
103+
104+
Make sure your CSP includes `style-src 'nonce-your-nonce-here'` and `script-src 'nonce-your-nonce-here'`.
105+
70106
## Documentation
71107

72108
Find the full API reference on [official documentation](https://react-hot-toast.com/docs).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
},
9595
"dependencies": {
9696
"csstype": "^3.1.3",
97-
"goober": "^2.1.16"
97+
"goober": "^2.1.17"
9898
},
9999
"peerDependencies": {
100100
"react": ">=16",

pnpm-lock.yaml

Lines changed: 29 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/pages/docs/styling.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,55 @@ import { Toaster, ToastBar } from 'react-hot-toast';
128128
)}
129129
</Toaster>;
130130
```
131+
132+
## Strict CSP Mode
133+
134+
For applications with strict Content Security Policies that disallow inline styles, enable `strictCSP` mode:
135+
136+
```jsx
137+
<Toaster strictCSP={true} />
138+
```
139+
140+
In strict CSP mode:
141+
- All inline `style` props are ignored
142+
- Styling must be done via CSS classes and CSS variables
143+
- Toast positioning uses CSS flexbox instead of inline transforms
144+
- The library is fully compatible with CSP `style-src 'nonce-...'` directives
145+
146+
### CSP Nonce Support
147+
148+
The library uses [goober](https://github.com/cristianbote/goober) for styling. To support CSP nonces, set `window.__nonce__` before your app loads:
149+
150+
```html
151+
<script nonce="your-nonce-here">
152+
window.__nonce__ = 'your-nonce-here';
153+
</script>
154+
```
155+
156+
goober will automatically apply the nonce to its generated `<style>` elements.
157+
158+
Make sure your CSP includes `style-src 'nonce-your-nonce-here'` and `script-src 'nonce-your-nonce-here'`.
159+
160+
### Styling with CSS Variables (Strict CSP)
161+
162+
When using strict CSP mode, use CSS variables for theming:
163+
164+
```css
165+
:root {
166+
/* Success toasts */
167+
--rht-success-bg: #ecfdf5;
168+
--rht-success-fg: #065f46;
169+
170+
/* Error toasts */
171+
--rht-error-bg: #fef2f2;
172+
--rht-error-fg: #991b1b;
173+
174+
/* Loading toasts */
175+
--rht-loading-bg: #eff6ff;
176+
--rht-loading-fg: #1e40af;
177+
178+
/* Blank toasts */
179+
--rht-blank-bg: #fff;
180+
--rht-blank-fg: #363636;
181+
}
182+
```

0 commit comments

Comments
 (0)