-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ThemeProvider: support applyTo prop (#14696)
* remove un-used prop * impl applyTo * Change files * fixes * merge * resolve comment * update api * call useFocusRects * resolve comment * udpate api
- Loading branch information
Showing
10 changed files
with
989 additions
and
27 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
change/@fluentui-react-theme-provider-2020-09-15-19-50-09-xgao-applyThemeToBody.json
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,8 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "Support applyTo prop and align styles with Fabric component.", | ||
"packageName": "@fluentui/react-theme-provider", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch", | ||
"date": "2020-09-16T02:50:09.294Z" | ||
} |
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
833 changes: 833 additions & 0 deletions
833
packages/react-theme-provider/src/__snapshots__/ThemeProvider.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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
60 changes: 55 additions & 5 deletions
60
packages/react-theme-provider/src/useThemeProviderClasses.tsx
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 |
---|---|---|
@@ -1,19 +1,69 @@ | ||
import { makeClasses } from './makeClasses'; | ||
import * as React from 'react'; | ||
import { css } from '@uifabric/utilities'; | ||
import { useDocument } from '@fluentui/react-window-provider'; | ||
import { IRawStyle } from '@uifabric/styling'; | ||
import { makeStyles } from './makeStyles'; | ||
import { ThemeProviderState } from './ThemeProvider.types'; | ||
import { tokensToStyleObject } from './tokensToStyleObject'; | ||
|
||
export const useThemeProviderClasses = makeClasses(theme => { | ||
const useThemeProviderStyles = makeStyles(theme => { | ||
const { tokens } = theme; | ||
const tokenStyles = tokensToStyleObject(tokens) as IRawStyle; | ||
|
||
return { | ||
root: [ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
tokensToStyleObject(tokens) as any, | ||
root: tokenStyles, | ||
body: [ | ||
{ | ||
color: 'var(--color-body-contentColor)', | ||
background: 'var(--color-body-background)', | ||
fontFamily: 'var(--body-fontFamily)', | ||
fontWeight: 'var(--body-fontWeight)', | ||
fontSize: 'var(--body-fontSize)', | ||
MozOsxFontSmoothing: 'var(--body-mozOsxFontSmoothing)', | ||
WebkitFontSmoothing: 'var(--body-webkitFontSmoothing)', | ||
}, | ||
], | ||
}; | ||
}); | ||
|
||
/** | ||
* Hook to add class to body element. | ||
*/ | ||
function useApplyClassToBody(state: ThemeProviderState, classesToApply: string[]): void { | ||
const { applyTo } = state; | ||
|
||
const applyToBody = applyTo === 'body'; | ||
const body = useDocument()?.body; | ||
|
||
React.useEffect(() => { | ||
if (!applyToBody || !body) { | ||
return; | ||
} | ||
|
||
for (const classToApply of classesToApply) { | ||
if (classToApply) { | ||
body.classList.add(classToApply); | ||
} | ||
} | ||
|
||
return () => { | ||
if (!applyToBody || !body) { | ||
return; | ||
} | ||
|
||
for (const classToApply of classesToApply) { | ||
if (classToApply) { | ||
body.classList.remove(classToApply); | ||
} | ||
} | ||
}; | ||
}, [applyToBody, body, classesToApply]); | ||
} | ||
|
||
export function useThemeProviderClasses(state: ThemeProviderState): void { | ||
const classes = useThemeProviderStyles(state.theme, state.renderer); | ||
useApplyClassToBody(state, [classes.root, classes.body]); | ||
|
||
const { className, applyTo } = state; | ||
state.className = css(className, classes.root, applyTo === 'element' && classes.body); | ||
} |