Skip to content

Commit 2b62f30

Browse files
authored
Merge pull request #908 from jpudysz/improve-notheme-error
fix: improve no theme error messages
2 parents 6e094fb + af25e04 commit 2b62f30

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/web/runtime.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,29 @@ export class UnistylesRuntime {
242242
}
243243

244244
getTheme = (themeName = this.themeName, CSSVars = false) => {
245+
const hasSomeThemes = this.services.state.themes.size > 0
246+
247+
if (!hasSomeThemes) {
248+
return new Proxy({} as UnistylesTheme, {
249+
get: () => {
250+
throw error('One of your stylesheets is trying to get the theme, but no theme has been selected yet. Did you forget to call StyleSheet.configure? If you called it, make sure you did so before any StyleSheet.create.')
251+
}
252+
})
253+
}
254+
255+
if (!themeName) {
256+
return new Proxy({} as UnistylesTheme, {
257+
get: () => {
258+
throw error('One of your stylesheets is trying to get the theme, but no theme has been selected yet. Did you forget to select an initial theme?')
259+
}
260+
})
261+
}
262+
245263
const theme = CSSVars
246264
? this.services.state.cssThemes.get(themeName ?? '')
247265
: this.services.state.themes.get(themeName ?? '')
248266

249-
if (!themeName || !theme) {
267+
if (!theme) {
250268
return new Proxy({} as UnistylesTheme, {
251269
get: () => {
252270
throw error(`You're trying to get theme "${themeName}" but it wasn't registered.`)

0 commit comments

Comments
 (0)