-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add getSlotClassNameProp to allow custom style hooks to preserve the original className while overriding the component default className. #34166
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
behowell
merged 19 commits into
microsoft:master
from
behowell:custom-style-hook-user-classname
Apr 16, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
188722c
Add overrideSlotClass function to allow custom style hooks to safely …
behowell b78f40c
change files
behowell a185c46
Rename to overrideDefaultSlotClasses
behowell 258227f
Rename to SLOT_ORIGINAL_CLASS_NAME_SYMBOL
behowell 2b2a330
Merge branch 'master' of https://github.com/microsoft/fluentui into c…
behowell 0286b71
Fix lint error
behowell a031887
Fix build break
behowell 51a972e
Replace utility function with getSlotClassNameProp
behowell 7828f62
Rename to getSlotOriginalClassNameProp and add documentation comment
behowell ecbb82c
Export from react-components
behowell 5947f06
Update Advanced Styling Techniques docsite page with the new function.
behowell b0a112e
Merge branch 'master' of https://github.com/microsoft/fluentui into c…
behowell 9cb05ab
Fix lint error
behowell f1a5518
rename to getSlotClassNameProp
behowell fef7fd7
Merge branch 'master' of https://github.com/microsoft/fluentui into c…
behowell 0622eaf
Revert public docsite change for now.
behowell bd4a614
Fix lint error
behowell ed3c9a4
Fix build break
behowell a5b0aeb
Merge branch 'master' into custom-style-hook-user-classname
behowell 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-components-6929107a-28e2-44fc-a7db-c26a1525e66a.json
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,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "Add getSlotClassNameProp_unstable function to allow custom style hooks to preserve the original className while overriding the component default className.", | ||
| "packageName": "@fluentui/react-components", | ||
| "email": "behowell@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-jsx-runtime-f24d1b2a-12c0-4040-baad-4d9cd5a2bebf.json
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,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "Support internal slot metadata change for SLOT_CLASS_NAME_PROP_SYMBOL", | ||
| "packageName": "@fluentui/react-jsx-runtime", | ||
| "email": "behowell@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-utilities-9a6dd4bb-e5d9-47b6-ab20-776b214c5d54.json
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,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "Add getSlotClassNameProp_unstable function to allow custom style hooks to preserve the original className while overriding the component default className.", | ||
| "packageName": "@fluentui/react-utilities", | ||
| "email": "behowell@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
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
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
30 changes: 30 additions & 0 deletions
30
packages/react-components/react-utilities/src/compose/getSlotClassNameProp.test.ts
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,30 @@ | ||
| import { getSlotClassNameProp } from './getSlotClassNameProp'; | ||
| import * as slot from './slot'; | ||
| import type { ComponentProps, Slot } from './types'; | ||
|
|
||
| type TestSlots = { | ||
| slotA: NonNullable<Slot<'div'>>; | ||
| }; | ||
|
|
||
| type TestProps = ComponentProps<TestSlots>; | ||
|
|
||
| const mergeClasses = (...classNames: (string | false | undefined)[]) => classNames.filter(Boolean).join(' '); | ||
|
|
||
| describe('getSlotClassNameProp', () => { | ||
| it('returns the original class name even if the slot className is modified', () => { | ||
| const props: TestProps = { slotA: { className: 'originalClassName' } }; | ||
| const slotA = slot.always(props.slotA, { elementType: 'div' }); | ||
| slotA.className = mergeClasses(slotA.className, 'overrideClassName'); | ||
|
|
||
| expect(getSlotClassNameProp(slotA)).toEqual('originalClassName'); | ||
| expect(slotA.className).toEqual('originalClassName overrideClassName'); | ||
| }); | ||
| it('returns undefined if the slot does not have a className', () => { | ||
| const props: TestProps = { slotA: {} }; | ||
| const slotA = slot.always(props.slotA, { elementType: 'div' }); | ||
| slotA.className = mergeClasses(slotA.className, 'overrideClassName'); | ||
|
|
||
| expect(getSlotClassNameProp(slotA)).toBeUndefined(); | ||
| expect(slotA.className).toEqual('overrideClassName'); | ||
| }); | ||
| }); |
24 changes: 24 additions & 0 deletions
24
packages/react-components/react-utilities/src/compose/getSlotClassNameProp.ts
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,24 @@ | ||
| import { SLOT_CLASS_NAME_PROP_SYMBOL } from '../compose/constants'; | ||
| import type { UnknownSlotProps } from '../compose/types'; | ||
|
|
||
| /** | ||
| * Get the className prop set on the slot by the user, without including the default classes added by the component. | ||
| * Custom style hooks should merge this className _after_ any additional classes added by the hook, to ensure that | ||
| * classes added by the user take precedence over the custom style hook. | ||
| * | ||
| * Example usage in a custom style hook: | ||
| * ```ts | ||
| * state.root.className = mergeClasses( | ||
| * state.root.className, | ||
| * customStyles.root, | ||
| * getSlotClassNameProp_unstable(state.root)); | ||
| * ``` | ||
| * | ||
| * @returns The className prop set on the slot by the user, or undefined if not set. | ||
| */ | ||
| export const getSlotClassNameProp = (slot: UnknownSlotProps) => { | ||
behowell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (SLOT_CLASS_NAME_PROP_SYMBOL in slot && typeof slot[SLOT_CLASS_NAME_PROP_SYMBOL] === 'string') { | ||
| return slot[SLOT_CLASS_NAME_PROP_SYMBOL]; | ||
| } | ||
| return undefined; | ||
| }; | ||
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
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.