-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Implement shared AvatarContext #24807
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
Changes from 5 commits
d14ff16
99a1e14
b58bce6
7d684fe
fa8dcb7
a1f6c92
08b8920
9fd4f3c
c7a14f5
3bfafde
18ba70d
e29a351
f792ca2
e48ff9c
bcf7dab
e211073
a847489
46153f1
d0a098d
ef70cc8
810164e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: consume shared AvatarContext", | ||
| "packageName": "@fluentui/react-avatar", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: Implement AvatarContext", | ||
| "packageName": "@fluentui/react-shared-contexts", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "feat: Use AvatarContext to override avatar size", | ||
| "packageName": "@fluentui/react-table", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "patch" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import * as React from 'react'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's up to you, but I think that it should be hosted in a separate package as
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is there any real issue with including it in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't mind, I've moved the avatar context to react-avatar in e29a351 |
||
|
|
||
| const avatarContext = React.createContext<AvatarContextValue | undefined>(undefined); | ||
|
|
||
| /** | ||
| * Sizes that can be used for the Avatar | ||
| * Copied from the react-avatar package, if the sizes are incompatible then TS should fail | ||
| */ | ||
| type AvatarSizes = 16 | 20 | 24 | 28 | 32 | 36 | 40 | 48 | 56 | 64 | 72 | 96 | 120 | 128; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export interface AvatarContextValue { | ||
| size?: AvatarSizes; | ||
| } | ||
|
|
||
| const avatarContextDefaultValue: AvatarContextValue = {}; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export const AvatarContextProvider = avatarContext.Provider; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export const useAvatarContext = () => React.useContext(avatarContext) ?? avatarContextDefaultValue; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,16 +1,22 @@ | ||||||||||||||||||
| import * as React from 'react'; | ||||||||||||||||||
| import { getSlots } from '@fluentui/react-utilities'; | ||||||||||||||||||
| import type { TableCellLayoutState, TableCellLayoutSlots } from './TableCellLayout.types'; | ||||||||||||||||||
| import { AvatarContextProvider } from '@fluentui/react-shared-contexts'; | ||||||||||||||||||
| import type { TableCellLayoutState, TableCellLayoutSlots, TableCellLayoutContextValues } from './TableCellLayout.types'; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Render the final JSX of TableCellLayout | ||||||||||||||||||
| */ | ||||||||||||||||||
| export const renderTableCellLayout_unstable = (state: TableCellLayoutState) => { | ||||||||||||||||||
| export const renderTableCellLayout_unstable = ( | ||||||||||||||||||
| state: TableCellLayoutState, | ||||||||||||||||||
| contextValues: TableCellLayoutContextValues, | ||||||||||||||||||
| ) => { | ||||||||||||||||||
| const { slots, slotProps } = getSlots<TableCellLayoutSlots>(state); | ||||||||||||||||||
|
|
||||||||||||||||||
| return ( | ||||||||||||||||||
| <slots.root {...slotProps.root}> | ||||||||||||||||||
| {slots.media && <slots.media {...slotProps.media} />} | ||||||||||||||||||
| <AvatarContextProvider value={contextValues.avatar}> | ||||||||||||||||||
| {slots.media && <slots.media {...slotProps.media} />} | ||||||||||||||||||
| </AvatarContextProvider> | ||||||||||||||||||
|
||||||||||||||||||
| <AvatarContextProvider value={contextValues.avatar}> | |
| {slots.media && <slots.media {...slotProps.media} />} | |
| </AvatarContextProvider> | |
| {slots.media && ( | |
| <AvatarContextProvider value={contextValues.avatar}> | |
| <slots.media {...slotProps.media} /> | |
| </AvatarContextProvider> | |
| )} |
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.
Fixed in e29a351
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import * as React from 'react'; | ||
| import type { TableCellLayoutState, TableCellLayoutContextValues } from './TableCellLayout.types'; | ||
|
|
||
| const tableAvatarSizeMap = { | ||
| medium: undefined, | ||
| small: 24, | ||
| smaller: 20, | ||
| } as const; | ||
|
|
||
| export function useTableCellLayoutContextValues_unstable(state: TableCellLayoutState): TableCellLayoutContextValues { | ||
| const { size: tableSize } = state; | ||
|
|
||
| const avatar = React.useMemo( | ||
| () => ({ | ||
| size: tableAvatarSizeMap[tableSize], | ||
|
||
| }), | ||
| [tableSize], | ||
| ); | ||
|
|
||
| return { | ||
| avatar, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,11 +77,7 @@ export const SizeSmall = () => { | |
| <TableCell> | ||
| <TableCellLayout | ||
| media={ | ||
| <Avatar | ||
| name={item.author.label} | ||
| badge={{ status: item.author.status as PresenceBadgeStatus }} | ||
| size={24} | ||
| /> | ||
| <Avatar name={item.author.label} badge={{ status: item.author.status as PresenceBadgeStatus }} /> | ||
|
||
| } | ||
| > | ||
| {item.author.label} | ||
|
|
||
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.
(nit) Update this description now that AvatarContext is implemented in react-avatar
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.
Updated in ef70cc8