-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ScreenSpinner): add subcomponents (#7250)
добавилены подкомпоненты и context для передачи общего состояния исправилена доступность state=cancelable (+ немного визуальное отображение - курсор теперь меняется только при наведении на сам спиннер с подложкой)
- Loading branch information
1 parent
0c8bb0b
commit 1dd7bbc
Showing
9 changed files
with
203 additions
and
101 deletions.
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 was deleted.
Oops, something went wrong.
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
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
172 changes: 132 additions & 40 deletions
172
packages/vkui/src/components/ScreenSpinner/ScreenSpinner.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 |
---|---|---|
@@ -1,69 +1,161 @@ | ||
import * as React from 'react'; | ||
import { Icon24Cancel } from '@vkontakte/icons'; | ||
import { classNames } from '@vkontakte/vkjs'; | ||
import { mergeCalls } from '../../lib/mergeCalls'; | ||
import { clickByKeyboardHandler } from '../../lib/utils'; | ||
import { HTMLAttributesWithRootRef } from '../../types'; | ||
import { useScrollLock } from '../AppRoot/ScrollContext'; | ||
import { PopoutWrapper } from '../PopoutWrapper/PopoutWrapper'; | ||
import { RootComponent } from '../RootComponent/RootComponent'; | ||
import { Spinner, SpinnerProps } from '../Spinner/Spinner'; | ||
import { Icon48CancelCircle } from './Icon48CancelCircle'; | ||
import { Icon48DoneOutline } from './Icon48DoneOutline'; | ||
import styles from './ScreenSpinner.module.css'; | ||
|
||
export interface ScreenSpinnerProps extends SpinnerProps { | ||
state?: 'loading' | 'cancelable' | 'done' | 'error'; | ||
cancelLabel?: string; | ||
} | ||
|
||
/** | ||
* @see https://vkcom.github.io/VKUI/#/ScreenSpinner | ||
*/ | ||
export const ScreenSpinner = ({ | ||
style, | ||
className, | ||
state = 'loading', | ||
export interface ScreenSpinnerContextProps { | ||
state: NonNullable<ScreenSpinnerProps['state']>; | ||
} | ||
|
||
export const ScreenSpinnerContext: React.Context<ScreenSpinnerContextProps> = | ||
React.createContext<ScreenSpinnerContextProps>({ | ||
state: 'loading', | ||
}); | ||
|
||
const stateClassNames = { | ||
cancelable: styles['ScreenSpinner--state-cancelable'], | ||
done: styles['ScreenSpinner--state-done'], | ||
error: styles['ScreenSpinner--state-error'], | ||
}; | ||
|
||
const ScreenSpinnerLoader: React.FC<SpinnerProps> = ({ | ||
size = 'large', | ||
onClick, | ||
children = 'Пожалуйста, подождите...', | ||
...restProps | ||
}: ScreenSpinnerProps): React.ReactNode => { | ||
const hideSpinner = state === 'done' || state === 'error'; | ||
}: SpinnerProps) => { | ||
return ( | ||
<Spinner className={styles['ScreenSpinner__spinner']} size={size} {...restProps}> | ||
{children} | ||
</Spinner> | ||
); | ||
}; | ||
|
||
ScreenSpinnerLoader.displayName = 'ScreenSpinner.Loader'; | ||
|
||
type ScreenSpinnerSwapIconProps = HTMLAttributesWithRootRef<HTMLElement> & { | ||
cancelLabel?: ScreenSpinnerProps['cancelLabel']; | ||
}; | ||
|
||
const ScreenSpinnerCancelIcon: React.FC<ScreenSpinnerSwapIconProps> = ({ | ||
onKeyDown, | ||
'aria-label': ariaLabel = 'Отменить', | ||
...restProps | ||
}: ScreenSpinnerSwapIconProps) => { | ||
const handlers = mergeCalls( | ||
{ onKeyDown: clickByKeyboardHandler }, | ||
{ | ||
onKeyDown, | ||
}, | ||
); | ||
let clickableProps: React.HTMLAttributes<HTMLSpanElement> = { | ||
...handlers, | ||
'tabIndex': 0, | ||
'role': 'button', | ||
'aria-label': ariaLabel, | ||
}; | ||
|
||
return ( | ||
<RootComponent baseClassName={styles['ScreenSpinner__icon']} {...clickableProps} {...restProps}> | ||
<Icon24Cancel /> | ||
</RootComponent> | ||
); | ||
}; | ||
|
||
const ScreenSpinnerSwapIcon: React.FC<ScreenSpinnerSwapIconProps> = ({ | ||
cancelLabel, | ||
...restProps | ||
}: ScreenSpinnerSwapIconProps) => { | ||
const { state } = React.useContext(ScreenSpinnerContext); | ||
|
||
if (state === 'cancelable') { | ||
return <ScreenSpinnerCancelIcon aria-label={cancelLabel} {...restProps} />; | ||
} | ||
|
||
const Icon = { | ||
loading: () => null, | ||
cancelable: Icon24Cancel, | ||
done: Icon48DoneOutline, | ||
error: Icon48CancelCircle, | ||
}[state]; | ||
|
||
return ( | ||
<RootComponent baseClassName={styles['ScreenSpinner__icon']} {...restProps}> | ||
<Icon /> | ||
</RootComponent> | ||
); | ||
}; | ||
|
||
ScreenSpinnerSwapIcon.displayName = 'ScreenSpinner.SwapIcon'; | ||
|
||
type ScreenSpinnerContainerProps = HTMLAttributesWithRootRef<HTMLSpanElement> & | ||
Pick<ScreenSpinnerProps, 'state'>; | ||
|
||
const ScreenSpinnerContainer: React.FC<ScreenSpinnerContainerProps> = ({ | ||
state = 'loading', | ||
...restProps | ||
}: ScreenSpinnerContainerProps) => { | ||
return ( | ||
<ScreenSpinnerContext.Provider value={{ state }}> | ||
<RootComponent | ||
baseClassName={classNames( | ||
styles['ScreenSpinner'], | ||
state !== 'loading' && stateClassNames[state], | ||
)} | ||
{...restProps} | ||
/> | ||
</ScreenSpinnerContext.Provider> | ||
); | ||
}; | ||
|
||
ScreenSpinnerContainer.displayName = 'ScreenSpinner.Container'; | ||
|
||
/** | ||
* @see https://vkcom.github.io/VKUI/#/ScreenSpinner | ||
*/ | ||
export const ScreenSpinner: React.FC<ScreenSpinnerProps> & { | ||
Container: typeof ScreenSpinnerContainer; | ||
Loader: typeof ScreenSpinnerLoader; | ||
SwapIcon: typeof ScreenSpinnerSwapIcon; | ||
} = ({ | ||
style, | ||
className, | ||
state = 'loading', | ||
onClick, | ||
cancelLabel, | ||
...restProps | ||
}: ScreenSpinnerProps): React.ReactNode => { | ||
useScrollLock(); | ||
|
||
return ( | ||
<PopoutWrapper | ||
noBackground | ||
className={classNames( | ||
styles['ScreenSpinner'], | ||
state === 'cancelable' && styles['ScreenSpinner--clickable'], | ||
className, | ||
)} | ||
style={style} | ||
> | ||
<div className={styles['ScreenSpinner__container']} onClick={onClick}> | ||
<Spinner | ||
className={classNames( | ||
styles['ScreenSpinner__spinner'], | ||
hideSpinner && styles['ScreenSpinner__spinner--hidden'], | ||
)} | ||
size={size} | ||
{...restProps} | ||
> | ||
{children} | ||
</Spinner> | ||
<div | ||
className={classNames( | ||
styles['ScreenSpinner__icon'], | ||
state === 'done' && styles['ScreenSpinner__icon--state-done'], | ||
)} | ||
> | ||
<Icon /> | ||
</div> | ||
</div> | ||
<PopoutWrapper className={className} style={style} noBackground> | ||
<ScreenSpinnerContainer state={state}> | ||
<ScreenSpinnerLoader {...restProps} /> | ||
<ScreenSpinnerSwapIcon onClick={onClick} cancelLabel={cancelLabel} /> | ||
</ScreenSpinnerContainer> | ||
</PopoutWrapper> | ||
); | ||
}; | ||
|
||
ScreenSpinner.displayName = 'ScreenSpinner'; | ||
|
||
ScreenSpinner.Container = ScreenSpinnerContainer; | ||
ScreenSpinner.Container.displayName = 'ScreenSpinner.Container'; | ||
|
||
ScreenSpinner.Loader = ScreenSpinnerLoader; | ||
ScreenSpinner.Loader.displayName = 'ScreenSpinner.Loader'; | ||
|
||
ScreenSpinner.SwapIcon = ScreenSpinnerSwapIcon; | ||
ScreenSpinner.SwapIcon.displayName = 'ScreenSpinner.SwapIcon'; |
Oops, something went wrong.