-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2274 from graphcommerce-org/feature/GCOM-1382
Feature/gcom 1382
- Loading branch information
Showing
9 changed files
with
143 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphcommerce/next-ui': patch | ||
--- | ||
|
||
Created a cssFlags functionality to allow for conditional rendering based on stored flags in the localStorage |
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,5 @@ | ||
--- | ||
"@graphcommerce/next-ui": minor | ||
--- | ||
|
||
Add props to DarkLightModeThemeProvider to disable dark/light mode or to change the default ssr mode. Save user chosen mode in localStorage |
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
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
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
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,76 @@ | ||
export const FLAGS_STORAGE_KEY = 'gc-flags' | ||
export const FLAG_PREFIX = 'data' | ||
|
||
export function getCssFlagsInitScript() { | ||
return ( | ||
<script | ||
id='init-gc-flags' | ||
key='mui-color-scheme-init' | ||
// eslint-disable-next-line react/no-danger | ||
dangerouslySetInnerHTML={{ | ||
__html: `(function() { | ||
try { | ||
const flags = JSON.parse(localStorage.getItem('${FLAGS_STORAGE_KEY}') || '{}') | ||
Object.entries(flags).forEach(([key, val]) => { | ||
document.documentElement.setAttribute('data-' +key, typeof val === 'boolean' ? '' : val) | ||
}) | ||
} catch(e){}})();`, | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
function loadFlags() { | ||
const flags = JSON.parse(localStorage.getItem(FLAGS_STORAGE_KEY) || '{}') | ||
if (typeof flags !== 'object' && flags !== null) return {} | ||
return flags as Record<string, true | string> | ||
} | ||
|
||
function saveFlags(flags: Record<string, true | string>) { | ||
window.localStorage?.setItem(FLAGS_STORAGE_KEY, JSON.stringify(flags)) | ||
} | ||
|
||
export function removeCssFlag(flagName: string) { | ||
const flags = loadFlags() | ||
delete flags[flagName] | ||
document.documentElement.removeAttribute(`data-${flagName}`) | ||
saveFlags(flags) | ||
} | ||
|
||
export function setCssFlag(flagName: string, val: true | string) { | ||
document.documentElement.setAttribute(`data-${flagName}`, typeof val === 'boolean' ? '' : val) | ||
|
||
const flags = loadFlags() | ||
flags[flagName] = val | ||
saveFlags(flags) | ||
} | ||
|
||
/** | ||
* @deprecated flags are not intendend to be used in JS, so this should only be used for debugging purposes. | ||
*/ | ||
export function getCssFlag(flagName: string) { | ||
return loadFlags()[flagName] | ||
} | ||
|
||
/** | ||
* Easily create a CSS selector that only applies when a flag is set. | ||
* | ||
* Example: | ||
* | ||
* ```tsx | ||
* <Box sx={{ [cssFlagSelector('dark')]: { color: 'white' } }} /> | ||
* ``` | ||
*/ | ||
export const cssFlag = <T extends string>(flagName: T) => `html[data-${flagName}] &` as const | ||
|
||
/** | ||
* Easily create a CSS selector that only applies when a flag is not set. | ||
* | ||
* Example: | ||
* | ||
* ```tsx | ||
* <Box sx={{ [cssNotFlagSelector('dark')]: { color: 'black' } }} /> | ||
* ``` | ||
*/ | ||
export const cssNotFlag = <T extends string>(flagName: T) => | ||
`html:not([data-${flagName}]) &` as const |