You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/paste-website/src/pages/blog/2025-03-20-css-variables-ssr-ssg.mdx
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -105,7 +105,7 @@ This section will cover how we implemented this solution on the Paste documentat
105
105
106
106
### _documents.tsx
107
107
108
-
We added a script to the <InlineCode>head</InlineCode> of our <InlineCode>_documents.tsx</InlineCode> file to set the <InlineCode>data-theme</InlineCode> attribute on the <InlineCode>body</InlineCode> element element using a script. This script will check to see if they have a cookie set and if not check the systme prefernces to determine whether users prefer dark or light themes and apply the dark theme if they do. Otherwise we do not set a value. You will see in <InlineCode>_app.tsx</InlineCode> how we handle the default value.
108
+
We added a script to the <InlineCode>head</InlineCode> of our <InlineCode>_documents.tsx</InlineCode> file to set the <InlineCode>data-theme</InlineCode> attribute on the <InlineCode>body</InlineCode> element using a script. This script will check to see if users have a cookie set and if not check the system preferences to determine whether users prefer dark or light themes, and apply the dark theme if they do. Otherwise we do not set a value. You will see in <InlineCode>_app.tsx</InlineCode> how we handle the default value.
109
109
110
110
Here is the script that is found in <AnchorshowExternal={true}href="https://github.com/twilio-labs/paste/blob/main/packages/paste-website/src/pages/_document.tsx"><InlineCode>_documents.tsx</InlineCode></Anchor>:
111
111
@@ -132,9 +132,9 @@ Here is the script that is found in <Anchor showExternal={true} href="https://gi
132
132
133
133
### _app.tsx
134
134
135
-
In our <InlineCode>_app.tsx</InlineCode> file we import the CSS files that should be applues. We have 2, one that applies the values to root and is considered the default theme and the other that uses the new <InlineCode>tokens.data-theme.css</InlineCode> attribute to apply the theme based on the value set in the <InlineCode>data-theme</InlineCode> attribute.
135
+
In our <InlineCode>_app.tsx</InlineCode> file we import the CSS files that should be applied. We have 2: One that applies the values to root and is considered the default theme, and the other that uses the new <InlineCode>tokens.data-theme.css</InlineCode> attribute to apply the theme based on the value set in the <InlineCode>data-theme</InlineCode> attribute.
136
136
137
-
You can find the source code <AnchorshowExternal={true}href="https://github.com/twilio-labs/paste/blob/main/packages/paste-website/src/pages/_app.tsx">here</Anchor>.
137
+
You can find the <AnchorshowExternal={true}href="https://github.com/twilio-labs/paste/blob/main/packages/paste-website/src/pages/_app.tsx">source code for _app.tsx here</Anchor>.
138
138
139
139
<BoxmarginBottom="space60">
140
140
<CodeBlockWrapper>
@@ -149,7 +149,7 @@ Note here that the import <InlineCode>"@twilio-paste/design-tokens/dist/themes/t
149
149
150
150
#### ThemeProvider
151
151
152
-
In this same file we set the ThemeProvider to use CSS variables instead of the theme files we previously configured. Note that we only want to use CSS variables when encoutnering this issue and not as a standard. For all other use cases we recommend using the <InlineCode>theme</InlineCode> prop.
152
+
In this same file we set the ThemeProvider to use CSS variables instead of the theme files we previously configured. Note that we only want to use CSS variables when encountering this issue and not as a standard. For all other use cases we recommend using the <InlineCode>theme</InlineCode> prop.
153
153
154
154
<BoxmarginBottom="space60">
155
155
<CodeBlockWrapper>
@@ -163,7 +163,7 @@ In this same file we set the ThemeProvider to use CSS variables instead of the t
163
163
164
164
### Switching the theme
165
165
166
-
We have a hook that is used to switch the themes in <AnchorshowExternal={true}href="https://github.com/twilio-labs/paste/blob/main/packages/paste-website/src/hooks/useDarkMode.tsx"><InlineCode>useDarkMode.tsx</InlineCode></Anchor>. It is in the <InlineCode>setMode</InlineCode> function we handle setting the data attribute on the body element. This occurs when the user intentionally changes the theme using the theme switcher in the header as this will run client side it cannot be used to set the default value. That comes from the script in the <InlineCode>_document.tsx</InlineCode>.
166
+
We have a hook that is used to switch the themes in <AnchorshowExternal={true}href="https://github.com/twilio-labs/paste/blob/main/packages/paste-website/src/hooks/useDarkMode.tsx"><InlineCode>useDarkMode.tsx</InlineCode></Anchor>. In the <InlineCode>setMode</InlineCode> function, we handle setting the data attribute on the body element. This occurs when the user intentionally changes the theme using the theme switcher in the header. As this will run client side, it cannot be used to set the default value. That comes from the script in the <InlineCode>_document.tsx</InlineCode>.
Copy file name to clipboardExpand all lines: packages/paste-website/src/pages/theme/changing-theme.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,7 @@ Alternatively, Paste will automatically load the default theme's font via JavaSc
65
65
66
66
## SSR and SSG
67
67
68
-
If you are using server-side rendering (SSR) or static site generation (SSG), <strong>and</strong> you support theme switching you will need to use CSS variables and setting a body data-theme attribute instead to avoid theme flicker on initial renders. You can view our <Anchorhref="/blog/2025-03-20-css-variables-ssr-ssg/">blog post</Anchor> on this topic, dicussing the problem and demonstrating how we solved it in the Paste website.
68
+
If you are using server-side rendering (SSR) or static site generation (SSG), <strong>and</strong> you support theme switching, you will need to use CSS variables and setting a body data-theme attribute instead to avoid theme flicker on initial renders. You can view our <Anchorhref="/blog/2025-03-20-css-variables-ssr-ssg/">blog post</Anchor> on this topic, discussing the problem and demonstrating how we solved it in the Paste website.
0 commit comments