-
Notifications
You must be signed in to change notification settings - Fork 20
feat(plasma-web): Added TabsController
in order to use keyboard arrows
#687
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { FC, useRef, useEffect, useContext } from 'react'; | ||
import styled, { css } from 'styled-components'; | ||
import { TabItem as BaseTabItem, secondary, footnote2 } from '@sberdevices/plasma-core'; | ||
import type { TabItemProps as BaseTabItemProps } from '@sberdevices/plasma-core'; | ||
|
||
import { link, linkHover, linkActive } from '../../tokens'; | ||
|
||
import { TabsContext } from './TabsContext'; | ||
|
||
export interface TabItemProps extends BaseTabItemProps {} | ||
|
||
const StyledTabItem = styled(BaseTabItem)` | ||
${footnote2}; | ||
|
||
/* stylelint-disable-next-line number-max-precision */ | ||
padding: 1rem 1.3125rem; | ||
height: 3.75rem; | ||
|
||
/* stylelint-disable-next-line number-max-precision */ | ||
box-shadow: inset 0 -0.0625rem 0 rgba(0, 0, 0, 0.16); | ||
color: ${secondary}; | ||
|
||
transition: color 0.1s ease-in-out, box-shadow 0.3s ease-in-out; | ||
|
||
&:hover { | ||
color: ${linkHover}; | ||
} | ||
|
||
/** | ||
* Состояние активности | ||
*/ | ||
${({ isActive }) => | ||
isActive && | ||
css` | ||
color: ${link}; | ||
box-shadow: inset 0 -0.125rem 0 ${link}; | ||
`} | ||
|
||
&.focus-visible:focus { | ||
color: ${linkActive}; | ||
box-shadow: inset 0 -0.125rem 0 ${linkActive}; | ||
} | ||
`; | ||
|
||
/** | ||
* Элемент списка вкладок, недопустимо импользовать вне компонента Tabs. | ||
*/ | ||
export const TabItem: FC<TabItemProps> = (props) => { | ||
const ref = useRef<HTMLElement>(null); | ||
const { refs } = useContext(TabsContext); | ||
|
||
useEffect(() => { | ||
refs?.register(ref); | ||
return () => refs?.unregister(ref); | ||
}, [refs]); | ||
|
||
return <StyledTabItem ref={ref} {...props} />; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,10 @@ | ||
import styled, { css } from 'styled-components'; | ||
import { Tabs as BaseTabs, TabItem as BaseTabItem, accent, secondary, footnote2 } from '@sberdevices/plasma-core'; | ||
import type { TabsProps as BaseTabsProps, TabItemProps as BaseTabItemProps } from '@sberdevices/plasma-core'; | ||
import styled from 'styled-components'; | ||
import { Tabs as BaseTabs } from '@sberdevices/plasma-core'; | ||
import type { TabsProps as BaseTabsProps } from '@sberdevices/plasma-core'; | ||
|
||
export interface TabsProps extends BaseTabsProps {} | ||
|
||
/** | ||
* Контейнер вкладок. | ||
* Контейнер вкладок, основной компонент для пользовательской сборки вкладок. | ||
*/ | ||
export const Tabs = styled(BaseTabs)``; | ||
|
||
export interface TabItemProps extends BaseTabItemProps {} | ||
|
||
/** | ||
* Элемент списка, недопустимо импользовать вне компонента Tabs. | ||
*/ | ||
export const TabItem = styled(BaseTabItem)` | ||
${footnote2}; | ||
|
||
/* stylelint-disable-next-line number-max-precision */ | ||
padding: 1rem 1.3125rem; | ||
height: 3.75rem; | ||
|
||
/* stylelint-disable-next-line number-max-precision */ | ||
box-shadow: inset 0 -0.0625rem 0 rgba(0, 0, 0, 0.16); | ||
color: ${secondary}; | ||
|
||
transition: color 0.1s ease-in-out, box-shadow 0.1s ease-in-out; | ||
|
||
/** | ||
* Состояние активности | ||
*/ | ||
${({ isActive }) => | ||
isActive && | ||
css` | ||
color: ${accent}; | ||
box-shadow: inset 0 -0.125rem 0 ${accent}; | ||
`} | ||
`; |
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,22 @@ | ||
import { MutableRefObject, createContext } from 'react'; | ||
|
||
export class TabItemRefs { | ||
public items: MutableRefObject<HTMLElement | null>[] = []; | ||
|
||
public register(ref: MutableRefObject<HTMLElement | null>): number { | ||
this.items.push(ref); | ||
return this.items.length - 1; | ||
} | ||
|
||
public unregister(ref: MutableRefObject<HTMLElement | null>) { | ||
this.items.splice(this.items.indexOf(ref), 1); | ||
} | ||
} | ||
|
||
interface TabsState { | ||
refs?: TabItemRefs; | ||
} | ||
|
||
const initialValue: TabsState = {}; | ||
|
||
export const TabsContext = createContext<TabsState>(initialValue); |
96 changes: 96 additions & 0 deletions
96
packages/plasma-web/src/components/Tabs/TabsController.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,96 @@ | ||
import React, { ReactNode, useRef, useMemo, useCallback, useEffect } from 'react'; | ||
|
||
import { TabItemRefs, TabsContext } from './TabsContext'; | ||
import { Tabs, TabsProps } from './Tabs'; | ||
import { TabItem } from './TabItem'; | ||
|
||
export interface TabsControllerProps extends TabsProps { | ||
items: Array<{ label: string; contentLeft: ReactNode }>; | ||
index: number; | ||
onIndexChange: (index: number) => void; | ||
children?: never; | ||
} | ||
|
||
enum Keys { | ||
end = 35, | ||
home = 36, | ||
left = 37, | ||
right = 39, | ||
} | ||
|
||
/** | ||
* Контроллер вкладок. | ||
* Позволяет использовать клавиши ArrowLeft, ArrowRight, Home, End для навигации по вкладкам. | ||
*/ | ||
export const TabsController: React.FC<TabsControllerProps> = ({ stretch, disabled, items, index, onIndexChange }) => { | ||
const listRef = useRef<HTMLDivElement>(null); | ||
const refs = useMemo(() => new TabItemRefs(), []); | ||
|
||
const onItemFocus = useCallback( | ||
(event) => { | ||
const focusIndex = refs.items.findIndex((itemRef) => itemRef.current === event.target); | ||
|
||
if (focusIndex !== index) { | ||
onIndexChange?.(focusIndex); | ||
} | ||
}, | ||
[refs, index, onIndexChange], | ||
); | ||
|
||
useEffect(() => { | ||
const onKeyup = (event: KeyboardEvent) => { | ||
const focusIndex = refs.items.findIndex((itemRef) => itemRef.current === document.activeElement); | ||
const minIndex = 0; | ||
const maxIndex = refs.items.length - 1; | ||
let nextIndex; | ||
|
||
switch (event.keyCode) { | ||
case Keys.end: | ||
nextIndex = maxIndex; | ||
break; | ||
case Keys.left: | ||
nextIndex = focusIndex > minIndex ? focusIndex - 1 : maxIndex; | ||
break; | ||
case Keys.right: | ||
nextIndex = focusIndex < maxIndex ? focusIndex + 1 : minIndex; | ||
break; | ||
case Keys.home: | ||
nextIndex = minIndex; | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
refs.items[nextIndex].current?.focus(); | ||
}; | ||
|
||
if (listRef.current) { | ||
listRef.current.addEventListener('keyup', onKeyup); | ||
} | ||
return () => { | ||
if (listRef.current) { | ||
listRef.current.removeEventListener('keyup', onKeyup); | ||
} | ||
}; | ||
}, [refs]); | ||
|
||
return ( | ||
<TabsContext.Provider value={{ refs }}> | ||
<Tabs ref={listRef} stretch={stretch}> | ||
{items.map(({ label, contentLeft }, i) => ( | ||
<TabItem | ||
key={i} | ||
isActive={i === index} | ||
tabIndex={!disabled ? 0 : -1} | ||
contentLeft={contentLeft} | ||
onClick={() => !disabled && onIndexChange?.(i)} | ||
onFocus={onItemFocus} | ||
> | ||
{label} | ||
</TabItem> | ||
))} | ||
</Tabs> | ||
</TabsContext.Provider> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export { Tabs, TabItem } from './Tabs'; | ||
export type { TabsProps, TabItemProps } from './Tabs'; | ||
export { TabsController } from './TabsController'; | ||
export type { TabsControllerProps } from './TabsController'; | ||
export { Tabs } from './Tabs'; | ||
export type { TabsProps } from './Tabs'; | ||
export { TabItem } from './TabItem'; | ||
export type { TabItemProps } from './TabItem'; |
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.
Сейчас не совсем непонятно, почему у нас именно 2 компоненты - Tabs и TabsController, когда как у TabsController вроде как та же функциональность, но теперь есть возможность использовать стрелочки. Почему мы оставляем Tabs? И нормально ли, что у них api отличается - у TabsController(появляются items, itemsindex, onIndexChange)?
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.
Эти компоненты переименуются v2.0:
Tabs
=>TabsRoot
TabsController
=>Tabs
TabsRoot
&TabItem
будут использоваться для кастомной сборкиTabs
останется "умным" компонентом, с которым не нужно делать лишних настроек#699