-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow controlling Frosted theme with CustomEvents #42
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
198fd2e
temporary idea
jjantschulev eb79d9f
Cleaned up PR
jjantschulev 2bb4075
validate theme options before setting
jjantschulev 70176f2
fix eslint
jjantschulev e5a1beb
Fixed build errors
jjantschulev be64e69
add eslint ignores
jjantschulev 14bfbc4
Update packages/frosted-ui/src/use-theme-events.ts
jjantschulev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
'use client'; | ||
|
||
import React, { FC, useEffect } from 'react'; | ||
import { useThemeContext } from './theme'; | ||
import { ThemeOptions, themePropDefs } from './theme-options'; | ||
|
||
function validateThemeColor< | ||
K extends Exclude<keyof typeof themePropDefs, 'hasBackground'>, | ||
>( | ||
key: K, | ||
value: unknown, | ||
): value is (typeof themePropDefs)[K]['values'][number] { | ||
const validValues = themePropDefs[key].values; | ||
return validValues.includes(value as never); | ||
} | ||
|
||
function validateThemeOptions(detail: unknown) { | ||
if (typeof detail !== 'object' || !detail) return {}; | ||
|
||
const ret: Partial<ThemeOptions> = {}; | ||
|
||
const keysToCheck: readonly (keyof ThemeOptions)[] = [ | ||
'accentColor', | ||
'appearance', | ||
'dangerColor', | ||
'grayColor', | ||
'infoColor', | ||
'successColor', | ||
'warningColor', | ||
]; | ||
|
||
for (const key of keysToCheck) { | ||
if (!(key in detail)) continue; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
if (!validateThemeColor(key, (detail as any)[key])) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
console.warn(`Invalid value for ${key}: ${(detail as any)[key]}`); | ||
continue; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
ret[key] = (detail as any)[key]; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
export function useThemeEvents() { | ||
const { | ||
accentColor, | ||
appearance, | ||
dangerColor, | ||
grayColor, | ||
infoColor, | ||
successColor, | ||
warningColor, | ||
onAccentColorChange, | ||
onAppearanceChange, | ||
onDangerColorChange, | ||
onGrayColorChange, | ||
onInfoColorChange, | ||
onSuccessColorChange, | ||
onWarningColorChange, | ||
} = useThemeContext(); | ||
|
||
// Emit an event when the component is mounted. | ||
// Listen for theme changes from the outside. | ||
useEffect(() => { | ||
const listener = (e: Event) => { | ||
if (e instanceof CustomEvent) { | ||
const d = validateThemeOptions(e.detail); | ||
if (d.appearance) onAppearanceChange(d.appearance); | ||
if (d.accentColor) onAccentColorChange(d.accentColor); | ||
if (d.grayColor) onGrayColorChange(d.grayColor); | ||
if (d.infoColor) onInfoColorChange(d.infoColor); | ||
if (d.successColor) onSuccessColorChange(d.successColor); | ||
if (d.warningColor) onWarningColorChange(d.warningColor); | ||
if (d.dangerColor) onDangerColorChange(d.dangerColor); | ||
} | ||
}; | ||
document.documentElement.addEventListener('frosted-ui:set-theme', listener); | ||
|
||
// Let external code know that the component is mounted. | ||
const event = new CustomEvent('frosted-ui:mounted'); | ||
document.documentElement.dispatchEvent(event); | ||
|
||
return () => { | ||
document.documentElement.removeEventListener( | ||
'frosted-ui:set-theme', | ||
listener, | ||
); | ||
const event = new CustomEvent('frosted-ui:unmounted'); | ||
document.documentElement.dispatchEvent(event); | ||
}; | ||
}, []); | ||
|
||
// Emit an event when the theme is changed. | ||
React.useEffect(() => { | ||
const event = new CustomEvent('frosted-ui:on-theme-change', { | ||
detail: { | ||
appearance, | ||
accentColor, | ||
grayColor, | ||
infoColor, | ||
successColor, | ||
warningColor, | ||
dangerColor, | ||
}, | ||
}); | ||
document.documentElement.dispatchEvent(event); | ||
}, [ | ||
appearance, | ||
accentColor, | ||
grayColor, | ||
infoColor, | ||
successColor, | ||
warningColor, | ||
dangerColor, | ||
]); | ||
} | ||
|
||
export const WithThemeEvents: FC = () => { | ||
useThemeEvents(); | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!