-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(react-badge): add useBadgeBase_unstable hook #35811
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
115992d
feat(react-badge): add useBadgeBase_unstable, useCounterBadgeBase_uns…
dmytrokirpa beb6fb4
change file
dmytrokirpa 4f3a0a3
chore: fix lint errors in react-badge base hooks
dmytrokirpa a998ad3
chore: fix formatting and lint in react-badge
dmytrokirpa 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-badge-eff39565-e2dd-45e9-93f5-ed2afdd31651.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": "feat: add base hooks for Badge", | ||
| "packageName": "@fluentui/react-badge", | ||
| "email": "dmytrokirpa@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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| export type { BadgeProps, BadgeSlots, BadgeState } from './components/Badge/index'; | ||
| export type { BadgeBaseProps, BadgeProps, BadgeSlots, BadgeBaseState, BadgeState } from './components/Badge/index'; | ||
| export { | ||
| Badge, | ||
| badgeClassNames, | ||
| renderBadge_unstable, | ||
| useBadgeStyles_unstable, | ||
| useBadge_unstable, | ||
| useBadgeBase_unstable, | ||
| } from './components/Badge/index'; |
8 changes: 7 additions & 1 deletion
8
packages/react-components/react-badge/library/src/CounterBadge.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 |
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| export type { CounterBadgeProps, CounterBadgeState } from './components/CounterBadge/index'; | ||
| export type { | ||
| CounterBadgeProps, | ||
| CounterBadgeState, | ||
| CounterBadgeBaseProps, | ||
| CounterBadgeBaseState, | ||
| } from './components/CounterBadge/index'; | ||
| export { | ||
| CounterBadge, | ||
| counterBadgeClassNames, | ||
| useCounterBadgeStyles_unstable, | ||
| useCounterBadge_unstable, | ||
| useCounterBadgeBase_unstable, | ||
| } from './components/CounterBadge/index'; |
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
4 changes: 2 additions & 2 deletions
4
packages/react-components/react-badge/library/src/components/Badge/index.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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| export { Badge } from './Badge'; | ||
| // Explicit exports to omit BadgeCommons | ||
| export type { BadgeProps, BadgeSlots, BadgeState } from './Badge.types'; | ||
| export type { BadgeBaseProps, BadgeBaseState, BadgeProps, BadgeSlots, BadgeState } from './Badge.types'; | ||
| export { renderBadge_unstable } from './renderBadge'; | ||
| export { useBadge_unstable } from './useBadge'; | ||
| export { useBadge_unstable, useBadgeBase_unstable } from './useBadge'; | ||
| export { badgeClassNames, useBadgeStyles_unstable } from './useBadgeStyles.styles'; |
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
39 changes: 23 additions & 16 deletions
39
packages/react-components/react-badge/library/src/components/Badge/useBadge.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 |
|---|---|---|
| @@ -1,41 +1,48 @@ | ||
| 'use client'; | ||
|
|
||
| import * as React from 'react'; | ||
| import { getIntrinsicElementProps, slot } from '@fluentui/react-utilities'; | ||
| import type { BadgeProps, BadgeState } from './Badge.types'; | ||
| import type { BadgeBaseProps, BadgeBaseState, BadgeProps, BadgeState } from './Badge.types'; | ||
|
|
||
| /** | ||
| * Returns the props and state required to render the component | ||
| */ | ||
| export const useBadge_unstable = (props: BadgeProps, ref: React.Ref<HTMLElement>): BadgeState => { | ||
| const { | ||
| shape = 'circular', | ||
| size = 'medium', | ||
| iconPosition = 'before', | ||
| appearance = 'filled', | ||
| color = 'brand', | ||
| } = props; | ||
| const { shape = 'circular', size = 'medium', appearance = 'filled', color = 'brand', ...badgeProps } = props; | ||
|
|
||
| const state = useBadgeBase_unstable(badgeProps, ref as React.Ref<HTMLDivElement>); | ||
|
|
||
| const state: BadgeState = { | ||
| return { | ||
| ...state, | ||
| shape, | ||
| size, | ||
| iconPosition, | ||
| appearance, | ||
| color, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Base hook for Badge component, which manages state related to slots structure and ARIA attributes. | ||
| * | ||
| * @param props - User provided props to the Badge component. | ||
| * @param ref - User provided ref to be passed to the Badge component. | ||
| */ | ||
| export const useBadgeBase_unstable = (props: BadgeBaseProps, ref: React.Ref<HTMLDivElement>): BadgeBaseState => { | ||
| const { iconPosition = 'before' } = props; | ||
|
|
||
| return { | ||
| iconPosition, | ||
| components: { | ||
| root: 'div', | ||
| icon: 'span', | ||
| }, | ||
| root: slot.always( | ||
| getIntrinsicElementProps('div', { | ||
| // FIXME: | ||
| // `ref` is wrongly assigned to be `HTMLElement` instead of `HTMLDivElement` | ||
| // but since it would be a breaking change to fix it, we are casting ref to it's proper type | ||
| ref: ref as React.Ref<HTMLDivElement>, | ||
| ref, | ||
| ...props, | ||
| }), | ||
| { elementType: 'div' }, | ||
| ), | ||
| icon: slot.optional(props.icon, { elementType: 'span' }), | ||
| }; | ||
|
|
||
| return state; | ||
| }; |
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
9 changes: 7 additions & 2 deletions
9
packages/react-components/react-badge/library/src/components/CounterBadge/index.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 |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| export { CounterBadge } from './CounterBadge'; | ||
| export type { CounterBadgeProps, CounterBadgeState } from './CounterBadge.types'; | ||
| export { useCounterBadge_unstable } from './useCounterBadge'; | ||
| export type { | ||
| CounterBadgeBaseProps, | ||
| CounterBadgeBaseState, | ||
| CounterBadgeProps, | ||
| CounterBadgeState, | ||
| } from './CounterBadge.types'; | ||
| export { useCounterBadge_unstable, useCounterBadgeBase_unstable } from './useCounterBadge'; | ||
| export { counterBadgeClassNames, useCounterBadgeStyles_unstable } from './useCounterBadgeStyles.styles'; |
46 changes: 32 additions & 14 deletions
46
packages/react-components/react-badge/library/src/components/CounterBadge/useCounterBadge.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
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
10 changes: 8 additions & 2 deletions
10
packages/react-components/react-badge/library/src/components/PresenceBadge/index.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
Oops, something went wrong.
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.