-
Notifications
You must be signed in to change notification settings - Fork 36
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
feat: Add Toolbar component to teams-components #311
Draft
ling1726
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
ling1726:teams-components/toolbar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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
packages/teams-components/src/components/Toolbar/Toolbar.styles.ts
This file contains 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 @@ | ||
import { makeStyles } from '@fluentui/react-components'; | ||
|
||
export const useStyles = makeStyles({ | ||
root: { | ||
display: 'flex', | ||
}, | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/teams-components/src/components/Toolbar/Toolbar.test.tsx
This file contains 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,9 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { Toolbar } from './Toolbar'; | ||
|
||
describe('Toolbar', () => { | ||
it('should render', () => { | ||
render(<Toolbar />); | ||
}); | ||
}); |
87 changes: 87 additions & 0 deletions
87
packages/teams-components/src/components/Toolbar/Toolbar.tsx
This file contains 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,87 @@ | ||
import { | ||
mergeClasses, | ||
useArrowNavigationGroup, | ||
} from '@fluentui/react-components'; | ||
import * as React from 'react'; | ||
import { useStyles } from './Toolbar.styles'; | ||
import { StrictCssClass } from '../../strictStyles'; | ||
import { | ||
ToolbarItemRegistrationProvider, | ||
useInitItemRegistration, | ||
} from './itemRegistration'; | ||
|
||
export interface ToolbarProps { | ||
children: React.ReactNode; | ||
className?: StrictCssClass; | ||
} | ||
|
||
export const toolbarClassNames = { | ||
root: 'tco-Toolbar', | ||
}; | ||
|
||
export const Toolbar = React.forwardRef<HTMLDivElement, ToolbarProps>( | ||
(props, ref) => { | ||
const { children, className } = props; | ||
const styles = useStyles(); | ||
const registerItem = useInitItemRegistration(); | ||
const contextValue = React.useMemo( | ||
() => ({ registerItem }), | ||
[registerItem] | ||
); | ||
|
||
return ( | ||
<ToolbarItemRegistrationProvider value={contextValue}> | ||
<div | ||
role="toolbar" | ||
className={mergeClasses( | ||
toolbarClassNames.root, | ||
styles.root, | ||
className?.toString() | ||
)} | ||
ref={ref} | ||
{...useArrowNavigationGroup({ axis: 'both', circular: true })} | ||
> | ||
{children} | ||
</div> | ||
</ToolbarItemRegistrationProvider> | ||
); | ||
} | ||
); | ||
|
||
// TODO implement DOM validation API | ||
// const isAllowedToolbarItem = (el: HTMLElement) => { | ||
// return ( | ||
// el.classList.contains(toolbarButtonClassNames.root) || | ||
// el.classList.contains(toolbarDividerClassNames.root) || | ||
// el.classList.contains(toolbarMenuButtonClassNames.root) || | ||
// el.classList.contains(toolbarToggleButtonClassNames.root) | ||
// ); | ||
// }; | ||
// | ||
// const isPortalSpan = (el: HTMLElement) => { | ||
// return el.tagName === 'SPAN' && el.hasAttribute('hidden'); | ||
// }; | ||
// | ||
// const isTabsterDummy = (el: HTMLElement) => { | ||
// return el.hasAttribute('data-tabster-dummy'); | ||
// }; | ||
// | ||
// const validateToolbarItems = (root: HTMLElement) => { | ||
// const children = root.children; | ||
// for (const child of children) { | ||
// // TODO is this even possible? | ||
// if (!isHTMLElement(child)) { | ||
// continue; | ||
// } | ||
// | ||
// if ( | ||
// !isAllowedToolbarItem(child) && | ||
// !isPortalSpan(child) && | ||
// !isTabsterDummy(child) | ||
// ) { | ||
// throw new Error( | ||
// '@fluentui-contrib/teams-components::Toolbar::Use Toolbar components from @fluentui-contrib/teams-components package only' | ||
// ); | ||
// } | ||
// } | ||
// }; |
33 changes: 33 additions & 0 deletions
33
packages/teams-components/src/components/Toolbar/ToolbarButton.tsx
This file contains 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,33 @@ | ||
import * as React from 'react'; | ||
import { Button, type ButtonProps } from '../Button'; | ||
import { createStrictClass } from '../../strictStyles/createStrictClass'; | ||
import { useItemRegistration } from './itemRegistration'; | ||
import { useMergedRefs } from '@fluentui/react-components'; | ||
import { mergeStrictClasses } from '../../strictStyles/mergeStrictClasses'; | ||
|
||
export const toolbarButtonClassNames = { | ||
root: 'tco-ToolbarButton', | ||
}; | ||
|
||
export type ToolbarButtonProps = Omit<ButtonProps, 'className'>; | ||
|
||
const rootStrictClassName = createStrictClass(toolbarButtonClassNames.root); | ||
|
||
// TODO teams-components should reuse composition patterns | ||
export const ToolbarButton = React.forwardRef<HTMLButtonElement, ButtonProps>( | ||
(props, ref) => { | ||
const { ref: registerRef, styles } = useItemRegistration({ | ||
appearance: props.appearance, | ||
type: 'button', | ||
}); | ||
|
||
return ( | ||
<Button | ||
ref={useMergedRefs(ref, registerRef)} | ||
{...props} | ||
className={mergeStrictClasses(rootStrictClassName, styles.root)} | ||
data-appearance={props.appearance} | ||
/> | ||
); | ||
} | ||
); |
7 changes: 7 additions & 0 deletions
7
packages/teams-components/src/components/Toolbar/ToolbarDivider.styles.ts
This file contains 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 @@ | ||
import { makeStyles } from '@fluentui/react-components'; | ||
|
||
export const useStyles = makeStyles({ | ||
root: { | ||
flexGrow: 'unset', | ||
}, | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/teams-components/src/components/Toolbar/ToolbarDivider.tsx
This file contains 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,41 @@ | ||
import * as React from 'react'; | ||
import { | ||
useDividerStyles_unstable, | ||
useDivider_unstable, | ||
renderDivider_unstable, | ||
mergeClasses, | ||
useMergedRefs, | ||
} from '@fluentui/react-components'; | ||
import { useStyles } from './ToolbarDivider.styles'; | ||
import { useItemRegistration } from './itemRegistration'; | ||
|
||
export const toolbarDividerClassNames = { | ||
root: 'tco-ToolbarDivider', | ||
}; | ||
|
||
export const ToolbarDivider = React.forwardRef< | ||
HTMLDivElement, | ||
Record<string, never> | ||
>((props, ref) => { | ||
const styles = useStyles(); | ||
const { ref: registerRef, styles: itemRegistrationStyles } = | ||
useItemRegistration({ | ||
appearance: props.appearance, | ||
type: 'divider', | ||
}); | ||
const state = useDivider_unstable( | ||
{ | ||
...props, | ||
vertical: true, | ||
className: mergeClasses( | ||
toolbarDividerClassNames.root, | ||
styles.root, | ||
itemRegistrationStyles.root.toString() | ||
), | ||
}, | ||
useMergedRefs(ref, registerRef) | ||
); | ||
useDividerStyles_unstable(state); | ||
|
||
return renderDivider_unstable(state); | ||
}); |
37 changes: 37 additions & 0 deletions
37
packages/teams-components/src/components/Toolbar/ToolbarMenuButton.tsx
This file contains 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,37 @@ | ||
import * as React from 'react'; | ||
import { MenuButton, MenuButtonProps } from '../MenuButton'; | ||
import { createStrictClass } from '../../strictStyles/createStrictClass'; | ||
import { useItemRegistration } from './itemRegistration'; | ||
import { useMergedRefs } from '@fluentui/react-components'; | ||
import { mergeStrictClasses } from '../../strictStyles'; | ||
|
||
export const toolbarMenuButtonClassNames = { | ||
root: 'tco-ToolbarMenuButton', | ||
}; | ||
|
||
export type ToolbarMenuButtonProps = Omit< | ||
MenuButtonProps, | ||
'className' | 'menuIcon' | ||
>; | ||
|
||
const rootStrictClassName = createStrictClass(toolbarMenuButtonClassNames.root); | ||
|
||
// TODO teams-components should reuse composition patterns | ||
export const ToolbarMenuButton = React.forwardRef< | ||
HTMLButtonElement, | ||
MenuButtonProps | ||
>((props, ref) => { | ||
const { ref: registerRef, styles } = useItemRegistration({ | ||
appearance: props.appearance, | ||
type: 'button', | ||
}); | ||
return ( | ||
<MenuButton | ||
ref={useMergedRefs(ref, registerRef)} | ||
{...props} | ||
menuIcon={null} | ||
className={mergeStrictClasses(rootStrictClassName, styles.root)} | ||
data-appearance={props.appearance} | ||
/> | ||
); | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/teams-components/src/components/Toolbar/ToolbarToggleButton.tsx
This file contains 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,35 @@ | ||
import * as React from 'react'; | ||
import { ToggleButton, type ToggleButtonProps } from '../ToggleButton'; | ||
import { createStrictClass } from '../../strictStyles/createStrictClass'; | ||
import { useItemRegistration } from './itemRegistration'; | ||
import { useMergedRefs } from '@fluentui/react-components'; | ||
import { mergeStrictClasses } from '../../strictStyles'; | ||
|
||
export const toolbarToggleButtonClassNames = { | ||
root: 'tco-ToolbarToggleButton', | ||
}; | ||
|
||
export type ToolbarToggleButtonProps = Omit<ToggleButtonProps, 'className'>; | ||
|
||
const rootStrictClassName = createStrictClass( | ||
toolbarToggleButtonClassNames.root | ||
); | ||
|
||
// TODO teams-components should reuse composition patterns | ||
export const ToolbarToggleButton = React.forwardRef< | ||
HTMLButtonElement, | ||
ToggleButtonProps | ||
>((props, ref) => { | ||
const { ref: registerRef, styles } = useItemRegistration({ | ||
appearance: props.appearance, | ||
type: 'button', | ||
}); | ||
return ( | ||
<ToggleButton | ||
ref={useMergedRefs(ref, registerRef)} | ||
{...props} | ||
className={mergeStrictClasses(rootStrictClassName, styles.root)} | ||
data-appearance={props.appearance} | ||
/> | ||
); | ||
}); |
This file contains 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,5 @@ | ||
export * from './Toolbar'; | ||
export * from './ToolbarDivider'; | ||
export * from './ToolbarButton'; | ||
export * from './ToolbarToggleButton'; | ||
export * from './ToolbarMenuButton'; | ||
Comment on lines
+1
to
+5
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. This is too wild, let's be explicit |
32 changes: 32 additions & 0 deletions
32
packages/teams-components/src/components/Toolbar/itemRegistration.styles.ts
This file contains 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,32 @@ | ||
import { makeStrictStyles } from '../../strictStyles/makeStrictStyles'; | ||
|
||
export const itemRegistrationVars = { | ||
toolbarItemMarginInlineStart: '--toolbar-item-margin-inline-start', | ||
}; | ||
|
||
let propertyRegisterComplete = false; | ||
|
||
export const registerCustomProperties = (win: typeof globalThis) => { | ||
if (propertyRegisterComplete) { | ||
Comment on lines
+7
to
+10
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. Should be done per window? |
||
return; | ||
} | ||
|
||
try { | ||
win.CSS.registerProperty({ | ||
name: itemRegistrationVars.toolbarItemMarginInlineStart, | ||
syntax: '<length>', | ||
inherits: false, | ||
initialValue: '0px', | ||
}); | ||
} catch { | ||
// ignore multiple registration error | ||
} | ||
|
||
propertyRegisterComplete = true; | ||
}; | ||
|
||
export const useItemRegistrationStyles = makeStrictStyles({ | ||
root: { | ||
marginInlineStart: `var(${itemRegistrationVars.toolbarItemMarginInlineStart})`, | ||
}, | ||
}); |
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.
I would double check if we really want to reuse
Divider
there