-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(draggableTabs): Add new draggable tabs component #75025
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
7 commits
Select commit
Hold shift + click to select a range
d0bc8fe
Copy tabs dir to draggableTabs dir
47cc7a6
Add framer motion to tabs
dbd3932
Clean up some artifacts of previous changes
723f34e
remove reference to temporary tabs
811229c
Change tab padding to better match designs
39805e6
minor code clean up
fb6872b
Move draggableTabBar to issueList
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import type React from 'react'; | ||
| import {forwardRef} from 'react'; | ||
| import styled from '@emotion/styled'; | ||
| import type {AriaTabProps} from '@react-aria/tabs'; | ||
| import {useTab} from '@react-aria/tabs'; | ||
| import {useObjectRef} from '@react-aria/utils'; | ||
| import type {TabListState} from '@react-stately/tabs'; | ||
| import type {Node, Orientation} from '@react-types/shared'; | ||
|
|
||
| import {BaseTab} from 'sentry/components/tabs/tab'; | ||
|
|
||
| interface DraggableTabProps extends AriaTabProps { | ||
| item: Node<any>; | ||
| orientation: Orientation; | ||
| /** | ||
| * Whether this tab is overflowing the TabList container. If so, the tab | ||
| * needs to be visually hidden. Users can instead select it via an overflow | ||
| * menu. | ||
| */ | ||
| overflowing: boolean; | ||
| state: TabListState<any>; | ||
| } | ||
|
|
||
| export const DraggableTab = forwardRef( | ||
| ( | ||
| {item, state, orientation, overflowing}: DraggableTabProps, | ||
| forwardedRef: React.ForwardedRef<HTMLLIElement> | ||
| ) => { | ||
| const ref = useObjectRef(forwardedRef); | ||
|
|
||
| const { | ||
| key, | ||
| rendered, | ||
| props: {to, hidden}, | ||
| } = item; | ||
| const {tabProps, isSelected} = useTab({key, isDisabled: hidden}, state, ref); | ||
|
|
||
| return ( | ||
| <StyledBaseTab | ||
| tabProps={tabProps} | ||
| isSelected={isSelected} | ||
| to={to} | ||
| hidden={hidden} | ||
| orientation={orientation} | ||
| overflowing={overflowing} | ||
| ref={ref} | ||
| variant="filled" | ||
| > | ||
| <TabContentWrap>{rendered}</TabContentWrap> | ||
| </StyledBaseTab> | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| const StyledBaseTab = styled(BaseTab)` | ||
| padding: 2px 12px 2px 12px; | ||
| gap: 8px; | ||
| border-radius: 6px 6px 0px 0px; | ||
| border: 1px solid ${p => p.theme.gray200}; | ||
| opacity: 0px; | ||
| `; | ||
|
|
||
| const TabContentWrap = styled('span')` | ||
| display: flex; | ||
| align-items: center; | ||
| flex-direction: row; | ||
| gap: 6px; | ||
| `; |
251 changes: 251 additions & 0 deletions
251
static/app/components/draggableTabs/draggableTabList.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,251 @@ | ||
| import {useContext, useEffect, useMemo, useRef} from 'react'; | ||
| import styled from '@emotion/styled'; | ||
| import type {AriaTabListOptions} from '@react-aria/tabs'; | ||
| import {useTabList} from '@react-aria/tabs'; | ||
| import {useCollection} from '@react-stately/collections'; | ||
| import {ListCollection} from '@react-stately/list'; | ||
| import type {TabListStateOptions} from '@react-stately/tabs'; | ||
| import {useTabListState} from '@react-stately/tabs'; | ||
| import type {Node, Orientation} from '@react-types/shared'; | ||
| import {Reorder} from 'framer-motion'; | ||
|
|
||
| import type {SelectOption} from 'sentry/components/compactSelect'; | ||
| import {TabsContext} from 'sentry/components/tabs'; | ||
| import {OverflowMenu, useOverflowTabs} from 'sentry/components/tabs/tabList'; | ||
| import {tabsShouldForwardProp} from 'sentry/components/tabs/utils'; | ||
| import {space} from 'sentry/styles/space'; | ||
| import {browserHistory} from 'sentry/utils/browserHistory'; | ||
| import type {Tab} from 'sentry/views/issueList/draggableTabBar'; | ||
|
|
||
| import {DraggableTab} from './draggableTab'; | ||
| import type {DraggableTabListItemProps} from './item'; | ||
| import {Item} from './item'; | ||
|
|
||
| interface BaseDraggableTabListProps extends DraggableTabListProps { | ||
| items: DraggableTabListItemProps[]; | ||
| setTabs: (tabs: Tab[]) => void; | ||
| tabs: Tab[]; | ||
| } | ||
|
|
||
| function BaseDraggableTabList({ | ||
| hideBorder = false, | ||
| className, | ||
| outerWrapStyles, | ||
| tabs, | ||
| setTabs, | ||
| ...props | ||
| }: BaseDraggableTabListProps) { | ||
| const tabListRef = useRef<HTMLUListElement>(null); | ||
| const {rootProps, setTabListState} = useContext(TabsContext); | ||
| const { | ||
| value, | ||
| defaultValue, | ||
| onChange, | ||
| disabled, | ||
| orientation = 'horizontal', | ||
| keyboardActivation = 'manual', | ||
| ...otherRootProps | ||
| } = rootProps; | ||
|
|
||
| // Load up list state | ||
| const ariaProps = { | ||
| selectedKey: value, | ||
| defaultSelectedKey: defaultValue, | ||
| onSelectionChange: key => { | ||
| onChange?.(key); | ||
|
|
||
| // If the newly selected tab is a tab link, then navigate to the specified link | ||
| const linkTo = [...(props.items ?? [])].find(item => item.key === key)?.to; | ||
| if (!linkTo) { | ||
| return; | ||
| } | ||
| browserHistory.push(linkTo); | ||
| }, | ||
| isDisabled: disabled, | ||
| keyboardActivation, | ||
| ...otherRootProps, | ||
| ...props, | ||
| }; | ||
|
|
||
| const state = useTabListState(ariaProps); | ||
|
|
||
| const {tabListProps} = useTabList({orientation, ...ariaProps}, state, tabListRef); | ||
| useEffect(() => { | ||
| setTabListState(state); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [state.disabledKeys, state.selectedItem, state.selectedKey, props.children]); | ||
|
|
||
| // Detect tabs that overflow from the wrapper and put them in an overflow menu | ||
| const tabItemsRef = useRef<Record<string | number, HTMLLIElement | null>>({}); | ||
| const overflowTabs = useOverflowTabs({ | ||
| tabListRef, | ||
| tabItemsRef, | ||
| tabItems: props.items, | ||
| }); | ||
|
|
||
| const overflowMenuItems = useMemo(() => { | ||
| // Sort overflow items in the order that they appear in TabList | ||
| const sortedKeys = [...state.collection].map(item => item.key); | ||
| const sortedOverflowTabs = overflowTabs.sort( | ||
| (a, b) => sortedKeys.indexOf(a) - sortedKeys.indexOf(b) | ||
| ); | ||
|
|
||
| return sortedOverflowTabs.flatMap<SelectOption<string | number>>(key => { | ||
| const item = state.collection.getItem(key); | ||
|
|
||
| if (!item) { | ||
| return []; | ||
| } | ||
|
|
||
| return { | ||
| value: key, | ||
| label: item.props.children, | ||
| disabled: item.props.disabled, | ||
| textValue: item.textValue, | ||
| }; | ||
| }); | ||
| }, [state.collection, overflowTabs]); | ||
|
|
||
| return ( | ||
| <TabListOuterWrap style={outerWrapStyles}> | ||
| <Reorder.Group axis="x" values={tabs} onReorder={setTabs} as="div"> | ||
| <TabListWrap | ||
| {...tabListProps} | ||
| orientation={orientation} | ||
| hideBorder={hideBorder} | ||
| className={className} | ||
| ref={tabListRef} | ||
| > | ||
| {[...state.collection].map(item => ( | ||
| <Reorder.Item | ||
| key={item.key} | ||
| value={tabs.find(tab => tab.key === item.key)} | ||
| style={{display: 'flex', flexDirection: 'row'}} | ||
| > | ||
| <DraggableTab | ||
| key={item.key} | ||
| item={item} | ||
| state={state} | ||
| orientation={orientation} | ||
| overflowing={ | ||
| orientation === 'horizontal' && overflowTabs.includes(item.key) | ||
| } | ||
| ref={element => (tabItemsRef.current[item.key] = element)} | ||
| /> | ||
| {state.selectedKey !== item.key && | ||
| state.collection.getKeyAfter(item.key) !== state.selectedKey && ( | ||
| <TabDivider /> | ||
| )} | ||
| </Reorder.Item> | ||
| ))} | ||
| </TabListWrap> | ||
| </Reorder.Group> | ||
|
|
||
| {orientation === 'horizontal' && overflowMenuItems.length > 0 && ( | ||
| <OverflowMenu | ||
| state={state} | ||
| overflowMenuItems={overflowMenuItems} | ||
| disabled={disabled} | ||
| /> | ||
| )} | ||
| </TabListOuterWrap> | ||
| ); | ||
| } | ||
|
|
||
| const collectionFactory = (nodes: Iterable<Node<any>>) => new ListCollection(nodes); | ||
|
|
||
| export interface DraggableTabListProps | ||
| extends AriaTabListOptions<DraggableTabListItemProps>, | ||
| TabListStateOptions<DraggableTabListItemProps> { | ||
| setTabs: (tabs: Tab[]) => void; | ||
| tabs: Tab[]; | ||
| className?: string; | ||
| hideBorder?: boolean; | ||
| outerWrapStyles?: React.CSSProperties; | ||
| } | ||
|
|
||
| /** | ||
| * To be used as a direct child of the <Tabs /> component. See example usage | ||
| * in tabs.stories.js | ||
| */ | ||
| export function DraggableTabList({ | ||
| items, | ||
| tabs, | ||
| setTabs, | ||
| ...props | ||
| }: DraggableTabListProps) { | ||
| const collection = useCollection({items, ...props}, collectionFactory); | ||
|
|
||
| const parsedItems = useMemo( | ||
| () => [...collection].map(({key, props: itemProps}) => ({key, ...itemProps})), | ||
| [collection] | ||
| ); | ||
|
|
||
| /** | ||
| * List of keys of disabled items (those with a `disbled` prop) to be passed | ||
| * into `BaseTabList`. | ||
| */ | ||
| const disabledKeys = useMemo( | ||
| () => parsedItems.filter(item => item.disabled).map(item => item.key), | ||
| [parsedItems] | ||
| ); | ||
|
|
||
| return ( | ||
| <BaseDraggableTabList | ||
| tabs={tabs} | ||
| items={parsedItems} | ||
| disabledKeys={disabledKeys} | ||
| setTabs={setTabs} | ||
| {...props} | ||
| > | ||
| {item => <Item {...item} />} | ||
| </BaseDraggableTabList> | ||
| ); | ||
| } | ||
|
|
||
| DraggableTabList.Item = Item; | ||
|
|
||
| const TabDivider = styled('div')` | ||
| height: 50%; | ||
| width: 1px; | ||
| border-radius: 6px; | ||
| background-color: ${p => p.theme.gray200}; | ||
| margin: 9px auto; | ||
| `; | ||
|
|
||
| const TabListOuterWrap = styled('div')` | ||
| position: relative; | ||
| `; | ||
|
|
||
| const TabListWrap = styled('ul', { | ||
| shouldForwardProp: tabsShouldForwardProp, | ||
| })<{ | ||
| hideBorder: boolean; | ||
| orientation: Orientation; | ||
| }>` | ||
| position: relative; | ||
| display: grid; | ||
| padding: 0; | ||
| margin: 0; | ||
| list-style-type: none; | ||
| flex-shrink: 0; | ||
| padding-left: 15px; | ||
|
|
||
| ${p => | ||
| p.orientation === 'horizontal' | ||
| ? ` | ||
| grid-auto-flow: column; | ||
| justify-content: start; | ||
| gap: ${space(0.5)}; | ||
| ${!p.hideBorder && `border-bottom: solid 1px ${p.theme.border};`} | ||
| stroke-dasharray: 4, 3; | ||
| ` | ||
| : ` | ||
| height: 100%; | ||
| grid-auto-flow: row; | ||
| align-content: start; | ||
| gap: 1px; | ||
| padding-right: ${space(2)}; | ||
| ${!p.hideBorder && `border-right: solid 1px ${p.theme.border};`} | ||
| `}; | ||
| `; |
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,64 @@ | ||
| import {Fragment} from 'react'; | ||
| import styled from '@emotion/styled'; | ||
|
|
||
| import JSXNode from 'sentry/components/stories/jsxNode'; | ||
| import SizingWindow from 'sentry/components/stories/sizingWindow'; | ||
| import storyBook from 'sentry/stories/storyBook'; | ||
| import {DraggableTabBar} from 'sentry/views/issueList/draggableTabBar'; | ||
|
|
||
| const TabPanelContainer = styled('div')` | ||
| width: 90%; | ||
| height: 250px; | ||
| background-color: white; | ||
| `; | ||
|
|
||
| export default storyBook(DraggableTabBar, story => { | ||
| const TABS = [ | ||
| { | ||
| key: 'one', | ||
| label: 'Inbox', | ||
| content: <TabPanelContainer>This is the Inbox view</TabPanelContainer>, | ||
| }, | ||
| { | ||
| key: 'two', | ||
| label: 'For Review', | ||
| content: <TabPanelContainer>This is the For Review view</TabPanelContainer>, | ||
| }, | ||
| { | ||
| key: 'three', | ||
| label: 'Regressed', | ||
| content: <TabPanelContainer>This is the Regressed view</TabPanelContainer>, | ||
| }, | ||
| ]; | ||
|
|
||
| story('Default', () => ( | ||
| <Fragment> | ||
| <p> | ||
| You should be using all of <JSXNode name="Tabs" />, <JSXNode name="TabList" />,{' '} | ||
| <JSXNode name="TabList.Item" />, <JSXNode name="DroppableTabPanels" /> and | ||
| <JSXNode name="DroppableTabPanels.Item" /> components. | ||
| </p> | ||
| <p> | ||
| This will give you all kinds of accessibility and state tracking out of the box. | ||
| But you will have to render all tab content, including hooks, upfront. | ||
| </p> | ||
| <SizingWindow> | ||
| <TabBarContainer> | ||
| <DraggableTabBar | ||
| tabs={TABS} | ||
| tempTabContent={ | ||
| <TabPanelContainer>This is a temporary tab</TabPanelContainer> | ||
| } | ||
| /> | ||
| </TabBarContainer> | ||
| </SizingWindow> | ||
| </Fragment> | ||
| )); | ||
| }); | ||
|
|
||
| const TabBarContainer = styled('div')` | ||
| display: flex; | ||
| justify-content: start; | ||
| width: 90%; | ||
| height: 300px; | ||
| `; | ||
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,12 @@ | ||
| import {Item as _Item} from '@react-stately/collections'; | ||
| import type {ItemProps} from '@react-types/shared'; | ||
| import type {LocationDescriptor} from 'history'; | ||
|
|
||
| export interface DraggableTabListItemProps extends ItemProps<any> { | ||
| key: string | number; | ||
| disabled?: boolean; | ||
| hidden?: boolean; | ||
| to?: LocationDescriptor; | ||
| } | ||
|
Comment on lines
+5
to
+10
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. This is currently exactly the same as the current TabListItemProps, but there will eventually be new props added that are specific to the draggable version. |
||
|
|
||
| export const Item = _Item as (props: DraggableTabListItemProps) => JSX.Element; | ||
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.
This is fine for now, but generally story files should only import from the component they are demo'ing. So we just need to make sure to come back to this later.