This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
feat(bindings): useStyles hook #2217
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2954cbe
feat(bindings): useStyles hook
layershifter f15ccec
Merge branches 'feat/use-styles-hook' and 'master' of https://github.…
layershifter eddaca4
build it
layershifter e542b74
remove compose changes
layershifter 861e1fb
continue on this
layershifter 69edaee
-reverted changes to components
mnajdova 3405aa8
-typos
mnajdova c8f15ee
add initial docs
layershifter 98fc985
Merge branch 'feat/use-styles-hook' of https://github.com/stardust-ui…
layershifter ce9b464
remove todo
layershifter 9b31750
fix dependencies
layershifter c6f0841
fix docs
layershifter 120bbe3
fix dependencies
layershifter 31c4c4a
move tests, fix tests
layershifter 7adc3c3
wip, needs cleanup
layershifter 5d93849
-moving changes to different PRs
mnajdova d0c6bb8
-reverted style param required related changes
mnajdova 6c59aee
Merge branches 'feat/use-styles-hook' and 'master' of https://github.…
layershifter 2f74a10
fix UT
layershifter 7e3852b
Merge branches 'feat/use-styles-hook' and 'master' of https://github.…
layershifter bf3bbca
add changelog entry
layershifter dd952e7
enforce types
layershifter 0ce4806
fix TS issue
layershifter dc223cc
do not reexport packages
layershifter 759d108
restore changes
layershifter 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
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,85 @@ | ||
import { | ||
ComponentSlotStyle, | ||
ComponentSlotStylesPrepared, | ||
ComponentVariablesInput, | ||
DebugData, | ||
emptyTheme, | ||
} from '@fluentui/styles' | ||
import * as React from 'react' | ||
// @ts-ignore We have this export in package, but it is not present in typings | ||
import { ThemeContext } from 'react-fela' | ||
|
||
import { | ||
ComponentDesignProp, | ||
ComponentSlotClasses, | ||
RendererRenderRule, | ||
StylesContextValue, | ||
} from '../styles/types' | ||
import getStyles from '../styles/getStyles' | ||
|
||
type PrimitiveProps = Record<string, boolean | number | string | undefined> | ||
type UseStylesOptions<StyleProps extends PrimitiveProps> = { | ||
className?: string | ||
mapPropsToStyles?: () => StyleProps | ||
mapPropsToInlineStyles?: () => InlineStyleProps<StyleProps> | ||
rtl?: boolean | ||
} | ||
|
||
type InlineStyleProps<StyleProps> = { | ||
/** Additional CSS class name(s) to apply. */ | ||
className?: string | ||
|
||
design?: ComponentDesignProp | ||
|
||
/** Additional CSS styles to apply to the component instance. */ | ||
styles?: ComponentSlotStyle<StyleProps, any> // TODO: see if we can improve it | ||
|
||
/** Override for theme site variables to allow modifications of component styling via themes. */ | ||
variables?: ComponentVariablesInput | ||
} | ||
|
||
const defaultContext: StylesContextValue<{ renderRule: RendererRenderRule }> = { | ||
disableAnimations: false, | ||
renderer: { renderRule: () => '' }, | ||
theme: emptyTheme, | ||
_internal_resolvedComponentVariables: {}, | ||
} | ||
|
||
const useStyles = <StyleProps extends PrimitiveProps>( | ||
displayName: string, | ||
options: UseStylesOptions<StyleProps>, | ||
): [ComponentSlotClasses, ComponentSlotStylesPrepared] => { | ||
const context: StylesContextValue<{ renderRule: RendererRenderRule }> = | ||
React.useContext(ThemeContext) || defaultContext | ||
|
||
const { | ||
className = process.env.NODE_ENV === 'production' ? '' : 'no-classname-🙉', | ||
mapPropsToStyles = () => ({} as StyleProps), | ||
mapPropsToInlineStyles = () => ({} as InlineStyleProps<StyleProps>), | ||
rtl = false, | ||
} = options | ||
|
||
// Stores debug information for component. | ||
const debug = React.useRef<{ fluentUIDebug: DebugData | null }>({ fluentUIDebug: null }) | ||
const { classes, styles: resolvedStyles } = getStyles({ | ||
// Input values | ||
className, | ||
displayName, | ||
props: { | ||
...mapPropsToStyles(), | ||
...mapPropsToInlineStyles(), | ||
}, | ||
|
||
// Context values | ||
disableAnimations: context.disableAnimations, | ||
renderer: context.renderer, | ||
rtl, | ||
saveDebug: fluentUIDebug => (debug.current = { fluentUIDebug }), | ||
theme: context.theme, | ||
_internal_resolvedComponentVariables: context._internal_resolvedComponentVariables, | ||
}) | ||
|
||
return [classes, resolvedStyles] | ||
} | ||
|
||
export default useStyles |
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.
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.
Any ideas on how we could decouple this call from the concrete theme? It would be nice to be able to reuse the base components with a different design system (which would likely have a separate theme shape).
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 next stage goal 🙇♂️
This is actually even not
ThemeContext
as it contains everything, likerenderer
anddisableAnimations
. Currently we can't we have multiple contexts as the most of our components are class components 🙃I have a naive idea that we will be able to move out
renderer
to a separate context and use the same interface different engines, https://codesandbox.io/embed/t-rex-proto-1w8bp