-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance useTheme to provide default theme without ThemeProvider #105
Conversation
I'll solve build error and open it again. |
import styled from '@emotion/native'; | ||
import {withTheme} from '@emotion/react'; | ||
import {useTheme} from './theme/ThemeProvider'; | ||
|
||
interface Styles { | ||
containerStyle?: ViewStyle; |
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.
You may remove prefix
Style
when it is grouped under Styles
.
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.
Ok. I'll remove it in other PR. (because it is not related to this PR.)
Codecov Report
@@ Coverage Diff @@
## master #105 +/- ##
==========================================
- Coverage 95.53% 95.50% -0.04%
==========================================
Files 23 22 -1
Lines 582 578 -4
Branches 267 265 -2
==========================================
- Hits 556 552 -4
Misses 26 26 |
function createCtx<A>(defaultContext: A): CreateCtx<A> { | ||
const ctx = React.createContext<A>(defaultContext); | ||
|
||
function useCtx(): A { | ||
const c = React.useContext(ctx); | ||
|
||
if (!c) throw new Error('useCtx must be inside a Provider with a value'); | ||
|
||
return c; | ||
} | ||
const useCtx = (): A => React.useContext(ctx); |
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.
This is very clever~! However, I am worried that many devs will be curious that useTheme
works without a Provider
because usually context wasn't provided like that 🤔
I hope there is more thing related to this to read about.
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.
@hyochan Ok. I will write about it soon.
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.
Although I am curious about providing default context value, I think this PR
is still great to have.
@hyochan Maybe this helps about <Provider
value={{
media,
themeType,
changeThemeType,
theme,
colors,
}}>
<OriginalThemeProvider theme={{...theme, ...media}}>
{children}
</OriginalThemeProvider>
</Provider> It will be best if we can give default context value to Therefore, default theme can be only accessed with our own |
Description
In Short: Providing default theme using
withTheme
,default value
, etc... often fails. This PR solve this problem by enhancinguseTheme
indooboo-ui
. Now,useTheme
will at least return default theme(light) even if there aren'tThemeProvider
exists.Providing theme is complicated task. And providing default theme is harder.
dooboo-ui
aims to provide default theme even if user forget to wrap componets withThemeProvider
.Currently, theme providing is done by this.
and providing default theme(when
ThemeProvider
is not provided) is done by this.However, default theme is not applied as I mentioned in #104.
It is because
withTheme
fromemotion
provides{ }
when there aren't any theme to provide. Soprops.theme
is neverundefined
, resulting our defaultTheme never applied. See this line of emotion implementation.So, I made a workaround in #86 using
isEmptyObject
.But it is just a workaround. It should be much easier and effortless to do it.
So I re-write
useTheme
and providelight
to be default theme.Now, there's no need to have
theme prop
,withTheme HOC
, anddefault value
. All you need to do to get current theme is callinguseTheme
. This will reduce confusion a lot.In this PR, I refactor only
SwitchToggle
using newuseTheme
.Test Plan
none.
Related Issues
#104
Tests
none.
Checklist
yarn test
oryarn test -u
if you need to update snapshot.yarn lint