-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat(theme): migrate theme context and dropdowns to local implementation #638
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bfadd26
feat(theme): migrate theme context and dropdowns to local implementation
dharmesh-hemaram 9263876
Update apps/acf-options-page/src/context/theme-context.tsx
dharmesh-hemaram 9d64b23
feat(storage): check local storage before opening options tab on install
dharmesh-hemaram 5bb6152
refactor(theme): move ThemeContext definition to improve code organiz…
dharmesh-hemaram 0e6cb87
feat(theme): migrate theme context and dropdowns to local implementation
dharmesh-hemaram 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 hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
export * from './theme-button-dropdown'; | ||
export * from './theme-nav-dropdown'; |
43 changes: 43 additions & 0 deletions
43
apps/acf-options-page/src/components/theme/theme-button-dropdown.tsx
This file contains hidden or 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,43 @@ | ||
import { getStoredTheme, ThemeContext, tTheme } from '@acf-options-page/context'; | ||
import { useContext, useState } from 'react'; | ||
import { Dropdown, DropdownButton } from 'react-bootstrap'; | ||
import { DropDirection } from 'react-bootstrap/esm/DropdownContext'; | ||
|
||
interface TThemeDropdownProps { | ||
drop?: DropDirection; | ||
} | ||
|
||
export const ThemeButtonDropdown = (props: TThemeDropdownProps) => { | ||
const { drop } = props; | ||
const [theme, setTheme] = useState(getStoredTheme()); | ||
const { updateTheme } = useContext(ThemeContext); | ||
|
||
const onClickTheme = (theme: tTheme | null) => { | ||
setTheme(theme); | ||
updateTheme(theme); | ||
}; | ||
|
||
const getIcon = () => { | ||
if (theme === 'light') { | ||
return <i className='bi bi-sun' />; | ||
} | ||
if (theme === 'dark') { | ||
return <i className='bi bi-moon' />; | ||
} | ||
return <i className='bi bi-circle-half' />; | ||
}; | ||
|
||
return ( | ||
<DropdownButton title={getIcon()} drop={drop}> | ||
<Dropdown.Item active={theme === 'light'} onClick={() => onClickTheme('light')}> | ||
<i className='bi bi-sun' /> Light | ||
</Dropdown.Item> | ||
<Dropdown.Item active={theme === 'dark'} onClick={() => onClickTheme('dark')}> | ||
<i className='bi bi-moon' /> Dark | ||
</Dropdown.Item> | ||
<Dropdown.Item active={theme === null} onClick={() => onClickTheme(null)}> | ||
<i className='bi bi-circle-half' /> Auto | ||
</Dropdown.Item> | ||
</DropdownButton> | ||
); | ||
}; |
37 changes: 37 additions & 0 deletions
37
apps/acf-options-page/src/components/theme/theme-nav-dropdown.tsx
This file contains hidden or 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,37 @@ | ||
import { getStoredTheme, ThemeContext, tTheme } from '@acf-options-page/context'; | ||
import { useContext, useState } from 'react'; | ||
import { NavDropdown } from 'react-bootstrap'; | ||
|
||
export const ThemeNavDropdown = () => { | ||
const [theme, setTheme] = useState(getStoredTheme()); | ||
const { updateTheme } = useContext(ThemeContext); | ||
|
||
const onClickTheme = (theme: tTheme | null) => { | ||
setTheme(theme); | ||
dharmesh-hemaram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updateTheme(theme); | ||
}; | ||
|
||
const getIcon = () => { | ||
if (theme === 'light') { | ||
return <i className='bi bi-sun-fill'></i>; | ||
} | ||
if (theme === 'dark') { | ||
return <i className='bi bi-moon-stars-fill'></i>; | ||
} | ||
return <i className='bi bi-circle-half'></i>; | ||
}; | ||
|
||
return ( | ||
<NavDropdown id='nav-dropdown-dark-example' title={getIcon()} menuVariant='dark'> | ||
<NavDropdown.Item onClick={() => onClickTheme('light')} active={theme === 'light'} className='d-flex align-items-center'> | ||
<i className='bi bi-sun-fill me-2 opacity-50'></i> Light | ||
</NavDropdown.Item> | ||
<NavDropdown.Item onClick={() => onClickTheme('dark')} active={theme === 'dark'} className='d-flex align-items-center'> | ||
<i className='bi bi-moon-stars-fill me-2 opacity-50 '></i> Dark | ||
</NavDropdown.Item> | ||
<NavDropdown.Item onClick={() => onClickTheme(null)} active={theme === null} className='d-flex align-items-center'> | ||
<i className='bi bi-circle-half me-2 opacity-50 '></i> Auto | ||
</NavDropdown.Item> | ||
</NavDropdown> | ||
); | ||
}; |
This file contains hidden or 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 @@ | ||
export * from './theme-context'; |
This file contains hidden or 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,54 @@ | ||
import React, { createContext, PropsWithChildren, useMemo, useState } from 'react'; | ||
|
||
export type tTheme = 'light' | 'dark'; | ||
|
||
export const ThemeContext = createContext({ | ||
theme: localStorage.getItem('theme') || 'light', | ||
dharmesh-hemaram marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
updateTheme: (theme: tTheme | null) => { | ||
return theme; | ||
} | ||
}); | ||
|
||
export const getStoredTheme = () => localStorage.getItem('theme'); | ||
|
||
const removeStoredTheme = () => localStorage.removeItem('theme'); | ||
|
||
const setStoredTheme = (theme: tTheme) => localStorage.setItem('theme', theme); | ||
|
||
const getSystemPreferredTheme = (): tTheme => { | ||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; | ||
}; | ||
|
||
const getPreferredTheme = (): tTheme => { | ||
const storedTheme = getStoredTheme(); | ||
if (storedTheme) { | ||
return storedTheme as tTheme; | ||
} | ||
return getSystemPreferredTheme(); | ||
}; | ||
|
||
export const ThemeProvider: React.FC<PropsWithChildren> = ({ children }) => { | ||
const [theme, setTheme] = useState<tTheme>(getPreferredTheme()); | ||
|
||
const updateTheme = (theme: tTheme | null) => { | ||
if (theme === null) { | ||
removeStoredTheme(); | ||
const systemTheme = getSystemPreferredTheme(); | ||
setTheme(systemTheme); | ||
document.documentElement.setAttribute('data-bs-theme', systemTheme); | ||
} else { | ||
setStoredTheme(theme); | ||
setTheme(theme); | ||
document.documentElement.setAttribute('data-bs-theme', theme); | ||
} | ||
return theme; | ||
}; | ||
|
||
React.useEffect(() => { | ||
document.documentElement.setAttribute('data-bs-theme', theme); | ||
}, [theme]); | ||
|
||
const value = useMemo(() => ({ theme, updateTheme }), [theme]); | ||
|
||
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>; | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.