-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(react-infobutton): Add initial implementation #25247
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
sopranopillow
merged 15 commits into
microsoft:master
from
sopranopillow:info-button/initial-implementation
Nov 2, 2022
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
29c1a87
adding initial implementation
sopranopillow eee5466
Merge branch 'master' of https://www.github.com/microsoft/fluentui in…
sopranopillow c9c76d1
fixing syncpack error
sopranopillow 3f53fd9
updating to remove v9 button
sopranopillow 75bc33b
updating implementation to new way
sopranopillow 8630172
Merge branch 'master' of https://www.github.com/microsoft/fluentui in…
sopranopillow b225c04
typos
sopranopillow 7883f2b
removing missing story
sopranopillow 381d61c
fixing syncpack error
sopranopillow 2f9aa37
convert Popover to a slot
sopranopillow 754a7f3
cleaning up types
sopranopillow e80518f
adding requested chan geS:
sopranopillow ab0e929
updating spread-radius to be a token
sopranopillow 668d364
adding requested changes
sopranopillow eebe657
fixing syncpack error
sopranopillow 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
3 changes: 3 additions & 0 deletions
3
...ges/react-components/react-infobutton/src/components/InfoButton/DefaultInfoButtonIcon.tsx
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,3 @@ | ||
| import { Info12Regular, Info12Filled, bundleIcon } from '@fluentui/react-icons'; | ||
|
|
||
| export const DefaultInfoButtonIcon = bundleIcon(Info12Filled, Info12Regular); |
47 changes: 38 additions & 9 deletions
47
packages/react-components/react-infobutton/src/components/InfoButton/InfoButton.test.tsx
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,18 +1,47 @@ | ||
| import * as React from 'react'; | ||
| import { render } from '@testing-library/react'; | ||
| import { InfoButton } from './InfoButton'; | ||
| import { isConformant } from '../../common/isConformant'; | ||
| import { infoButtonClassNames } from './useInfoButtonStyles'; | ||
| import type { RenderResult } from '@testing-library/react'; | ||
|
|
||
| // testing-library's queryByRole function doesn't look inside portals | ||
| function queryByRoleDialog(result: RenderResult) { | ||
| const dialogs = result.baseElement.querySelectorAll('[role="dialog"]'); | ||
| if (!dialogs?.length) { | ||
| return null; | ||
| } else { | ||
| expect(dialogs.length).toBe(1); | ||
| return dialogs.item(0) as HTMLElement; | ||
| } | ||
| } | ||
|
|
||
| const getPopoverSurfaceElement = (result: RenderResult) => { | ||
| // button needs to be clicked otherwise content won't be rendered. | ||
| result.getByRole('button').click(); | ||
| const dialog = queryByRoleDialog(result); | ||
| expect(dialog).not.toBeNull(); | ||
| return dialog!; | ||
| }; | ||
|
|
||
| describe('InfoButton', () => { | ||
| isConformant({ | ||
| Component: InfoButton, | ||
| displayName: 'InfoButton', | ||
| }); | ||
|
|
||
| // TODO add more tests here, and create visual regression tests in /apps/vr-tests | ||
|
|
||
| it('renders a default state', () => { | ||
| const result = render(<InfoButton>Default InfoButton</InfoButton>); | ||
| expect(result.container).toMatchSnapshot(); | ||
| requiredProps: { | ||
| content: 'Popover content', | ||
| }, | ||
| testOptions: { | ||
| 'has-static-classnames': [ | ||
| { | ||
| props: { | ||
| content: 'Popover content', | ||
| }, | ||
| expectedClassNames: { | ||
| root: infoButtonClassNames.root, | ||
| content: infoButtonClassNames.content, | ||
| }, | ||
| getPortalElement: getPopoverSurfaceElement, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| }); |
6 changes: 3 additions & 3 deletions
6
packages/react-components/react-infobutton/src/components/InfoButton/InfoButton.tsx
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
15 changes: 13 additions & 2 deletions
15
packages/react-components/react-infobutton/src/components/InfoButton/InfoButton.types.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
11 changes: 0 additions & 11 deletions
11
...ponents/react-infobutton/src/components/InfoButton/__snapshots__/InfoButton.test.tsx.snap
This file was deleted.
Oops, something went wrong.
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
28 changes: 0 additions & 28 deletions
28
packages/react-components/react-infobutton/src/components/InfoButton/useInfoButton.ts
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
packages/react-components/react-infobutton/src/components/InfoButton/useInfoButton.tsx
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,58 @@ | ||
| import * as React from 'react'; | ||
| import { DefaultInfoButtonIcon } from './DefaultInfoButtonIcon'; | ||
| import { getNativeElementProps, mergeCallbacks, resolveShorthand } from '@fluentui/react-utilities'; | ||
| import { Popover, PopoverSurface } from '@fluentui/react-popover'; | ||
| import { useControllableState } from '@fluentui/react-utilities'; | ||
| import type { InfoButtonProps, InfoButtonState } from './InfoButton.types'; | ||
|
|
||
| /** | ||
| * Create the state required to render InfoButton. | ||
| * | ||
| * The returned state can be modified with hooks such as useInfoButtonStyles_unstable, | ||
| * before being passed to renderInfoButton_unstable. | ||
| * | ||
| * @param props - props from this instance of InfoButton | ||
| * @param ref - reference to root HTMLElement of InfoButton | ||
| */ | ||
| export const useInfoButton_unstable = (props: InfoButtonProps, ref: React.Ref<HTMLElement>): InfoButtonState => { | ||
| const state: InfoButtonState = { | ||
| components: { | ||
| root: 'button', | ||
| popover: Popover, | ||
| content: PopoverSurface, | ||
| }, | ||
|
|
||
| root: getNativeElementProps('button', { | ||
| children: <DefaultInfoButtonIcon />, | ||
| type: 'button', | ||
| ...props, | ||
| ref, | ||
| }), | ||
| popover: resolveShorthand(props.popover, { | ||
| required: true, | ||
| defaultProps: { | ||
| children: <></>, | ||
| positioning: 'above-start', | ||
| size: 'small', | ||
| withArrow: true, | ||
| }, | ||
| }), | ||
| content: resolveShorthand(props.content, { | ||
| required: true, | ||
| defaultProps: { | ||
| role: 'dialog', | ||
| }, | ||
| }), | ||
| }; | ||
|
|
||
| const [popoverOpen, setPopoverOpen] = useControllableState({ | ||
| state: state.popover.open, | ||
| defaultState: state.popover.defaultOpen, | ||
| initialState: false, | ||
| }); | ||
|
|
||
| state.popover.open = popoverOpen; | ||
| state.popover.onOpenChange = mergeCallbacks(state.popover.onOpenChange, (e, data) => setPopoverOpen(data.open)); | ||
|
|
||
| return state; | ||
| }; | ||
96 changes: 85 additions & 11 deletions
96
packages/react-components/react-infobutton/src/components/InfoButton/useInfoButtonStyles.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,33 +1,107 @@ | ||
| import { makeStyles, mergeClasses } from '@griffel/react'; | ||
| import { createCustomFocusIndicatorStyle } from '@fluentui/react-tabster'; | ||
| import { iconFilledClassName, iconRegularClassName } from '@fluentui/react-icons'; | ||
| import { makeStyles, mergeClasses, shorthands } from '@griffel/react'; | ||
| import { tokens } from '@fluentui/react-theme'; | ||
| import type { InfoButtonSlots, InfoButtonState } from './InfoButton.types'; | ||
| import type { SlotClassNames } from '@fluentui/react-utilities'; | ||
|
|
||
| export const infoButtonClassNames: SlotClassNames<InfoButtonSlots> = { | ||
| root: 'fui-InfoButton', | ||
| // TODO: add class names for all slots on InfoButtonSlots. | ||
| // Should be of the form `<slotName>: 'fui-InfoButton__<slotName>` | ||
| // this className won't be used, but it's needed to satisfy the type checker | ||
| popover: 'fui-InfoButton__popover', | ||
| content: 'fui-InfoButton__content', | ||
| }; | ||
|
|
||
| /** | ||
| * Styles for the root slot | ||
| */ | ||
| const useStyles = makeStyles({ | ||
| root: { | ||
| // TODO Add default styles for the root element | ||
| const useButtonStyles = makeStyles({ | ||
| base: { | ||
| alignItems: 'center', | ||
| boxSizing: 'border-box', | ||
| display: 'inline-flex', | ||
| justifyContent: 'center', | ||
| textDecorationLine: 'none', | ||
| verticalAlign: 'middle', | ||
|
|
||
| backgroundColor: tokens.colorTransparentBackground, | ||
| color: tokens.colorNeutralForeground2, | ||
| fontFamily: tokens.fontFamilyBase, | ||
|
|
||
| ...shorthands.overflow('hidden'), | ||
| ...shorthands.border(tokens.strokeWidthThin, 'solid', tokens.colorTransparentStroke), | ||
| ...shorthands.padding(tokens.spacingVerticalXS, tokens.spacingHorizontalXS), | ||
| ...shorthands.margin(0), | ||
|
|
||
| [`& .${iconFilledClassName}`]: { | ||
| display: 'none', | ||
| }, | ||
| [`& .${iconRegularClassName}`]: { | ||
| display: 'inline-flex', | ||
| }, | ||
|
|
||
| ':enabled:hover': { | ||
| backgroundColor: tokens.colorTransparentBackgroundHover, | ||
| color: tokens.colorNeutralForeground2BrandHover, | ||
|
|
||
| [`& .${iconFilledClassName}`]: { | ||
| display: 'inline-flex', | ||
| }, | ||
| [`& .${iconRegularClassName}`]: { | ||
| display: 'none', | ||
| }, | ||
sopranopillow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| ':enabled:hover:active': { | ||
| backgroundColor: tokens.colorTransparentBackgroundPressed, | ||
| color: tokens.colorNeutralForeground2BrandPressed, | ||
| }, | ||
| ':disabled': { | ||
| cursor: 'not-allowed', | ||
| color: tokens.colorNeutralForegroundDisabled, | ||
| }, | ||
| }, | ||
|
|
||
| // TODO add additional classes for different states and/or slots | ||
| focusIndicator: createCustomFocusIndicatorStyle({ | ||
| ...shorthands.borderRadius(tokens.borderRadiusSmall), | ||
| ...shorthands.borderColor(tokens.colorTransparentStroke), | ||
| outlineColor: tokens.colorTransparentStroke, | ||
| outlineWidth: tokens.strokeWidthThick, | ||
| outlineStyle: 'solid', | ||
| boxShadow: ` | ||
| ${tokens.shadow4}, | ||
| 0 0 0 ${tokens.borderRadiusSmall} ${tokens.colorStrokeFocus2} | ||
| `, | ||
| zIndex: 1, | ||
| }), | ||
|
|
||
| selected: { | ||
| backgroundColor: tokens.colorTransparentBackgroundSelected, | ||
| color: tokens.colorNeutralForeground2BrandSelected, | ||
|
|
||
| [`& .${iconFilledClassName}`]: { | ||
| display: 'inline-flex', | ||
| }, | ||
| [`& .${iconRegularClassName}`]: { | ||
| display: 'none', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Apply styling to the InfoButton slots based on the state | ||
| */ | ||
| export const useInfoButtonStyles_unstable = (state: InfoButtonState): InfoButtonState => { | ||
| const styles = useStyles(); | ||
| state.root.className = mergeClasses(infoButtonClassNames.root, styles.root, state.root.className); | ||
| const { open } = state.popover; | ||
| const buttonStyles = useButtonStyles(); | ||
|
|
||
| // TODO Add class names to slots, for example: | ||
| // state.mySlot.className = mergeClasses(styles.mySlot, state.mySlot.className); | ||
| state.content.className = mergeClasses(infoButtonClassNames.content, state.content.className); | ||
| state.root.className = mergeClasses( | ||
| infoButtonClassNames.root, | ||
| buttonStyles.base, | ||
| buttonStyles.focusIndicator, | ||
| open && buttonStyles.selected, | ||
| state.root.className, | ||
| ); | ||
|
|
||
| return state; | ||
| }; | ||
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.
Is there a reason this needs to be set? I would expect if there are no children for
childrento beundefined.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.
Yes, children cannot be undefined, so this is needed.