Replies: 3 comments 3 replies
-
We could let the theme switch immediately and let the requests happen in the background. |
Beta Was this translation helpful? Give feedback.
-
We could definitely implement optimistic UI right here: Using |
Beta Was this translation helpful? Give feedback.
-
Been experimenting with the useFetchers idea. This is what I have so far and it seems to work pretty well. export function useTheme() {
const requestInfo = useRequestInfo()
const { optimisticTheme, hints } = useOptimisticTheme()
return optimisticTheme ?? requestInfo.session.theme ?? hints.theme
}
function useOptimisticTheme() {
const hints = useHints()
const fetchers = useFetchers()
const themeFetcher = fetchers.find(f => f.formAction?.startsWith(ROUTE_PATH))
let themeSubmitted
let optimisticTheme
if (themeFetcher && themeFetcher.formData) {
const submission = parse(themeFetcher.formData, {
schema: ThemeFormSchema,
})
themeSubmitted = submission.value?.theme
optimisticTheme = themeSubmitted === 'system' ? hints.theme : themeSubmitted
}
return { optimisticTheme, themeSubmitted, hints }
} It may be a little over-engineered, not sure. Dealing with the "system" option complicates things as that needs something to fall back on. Not sure if using the hints for that is the best option but it seems to work. Also, the reason I used a seperate function was to be able to reuse it up in the const { themeSubmitted } = useOptimisticTheme()
const mode = themeSubmitted ?? userPreference ?? 'system' And I suppose this would theoretically allow it to be used anywhere you need optimistic UI related to the theme. |
Beta Was this translation helpful? Give feedback.
-
Hello epic-stack enjoyers. 👋
I have been experimenting with this stack a lot recently and am loving it. However, I have come across an issue regarding the theme switching functionality.
If the user has a slow connection, the theme switch takes quite some time to happen. Here is an example of a theme switch action in my network tab using Fast 3G throttling.
With the current theme switch implementation, the theme will not change until both of those network requests are finished.
I initially discovered this while trying to make a serverless version of this stack using Planetscale as a database. Even with the ~100 ms server responses in that environment, the theme toggling felt clunky. This is obviously less than ideal from a UX perspective, so I was wondering if there is any way around this?
Beta Was this translation helpful? Give feedback.
All reactions