Skip to content

Commit

Permalink
refactor(Popover): mv to stable
Browse files Browse the repository at this point in the history
  • Loading branch information
inomdzhon committed Nov 22, 2023
1 parent 29ccf9e commit 1563164
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 197 deletions.
18 changes: 11 additions & 7 deletions packages/vkui/src/components/FocusTrap/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface FocusTrapProps<T extends HTMLElement = HTMLElement>
extends React.AllHTMLAttributes<T>,
HasRootRef<T>,
HasComponent {
restoreFocus?: boolean;
autoFocus?: boolean;
restoreFocus?: boolean | (() => boolean);
timeout?: number;
onClose?(): void;
}
Expand All @@ -24,6 +25,7 @@ export interface FocusTrapProps<T extends HTMLElement = HTMLElement>
export const FocusTrap = <T extends HTMLElement = HTMLElement>({
Component = 'div',
onClose,
autoFocus = true,
restoreFocus = true,
timeout = 0,
getRootRef,
Expand All @@ -50,8 +52,10 @@ export const FocusTrap = <T extends HTMLElement = HTMLElement>({
}
}, timeout);
useIsomorphicLayoutEffect(() => {
focusOnTrapMount.set();
}, []);
if (autoFocus) {
focusOnTrapMount.set();
}
}, [autoFocus]);

// HANDLE FOCUSABLE NODES

Expand Down Expand Up @@ -84,17 +88,17 @@ export const FocusTrap = <T extends HTMLElement = HTMLElement>({
// HANDLE TRAP UNMOUNT

const focusOnTrapUnmount = useTimeout(() => {
if (restoreFocusTo) {
const shouldRestore = typeof restoreFocus === 'function' ? restoreFocus() : restoreFocus;
if (shouldRestore && restoreFocusTo) {
restoreFocusTo.focus();
}
}, timeout);

useIsomorphicLayoutEffect(() => {
if (restoreFocus && document!.activeElement) {
setRestoreFocusTo(document!.activeElement as HTMLElement);

return () => {
focusOnTrapUnmount.set();
};
return focusOnTrapUnmount.set;
}

return;
Expand Down
11 changes: 0 additions & 11 deletions packages/vkui/src/components/Popover/Popover.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.Popover {
position: relative;
animation: popover-fade-in 0.2s ease;
background: var(--vkui--color_background_modal);
border-radius: var(--vkui--size_border_radius--regular);
box-shadow: var(--vkui--elevation3);
Expand All @@ -19,13 +18,3 @@
.Popover__in {
position: relative;
}

@keyframes popover-fade-in {
from {
opacity: 0;
}

to {
opacity: 1;
}
}
43 changes: 40 additions & 3 deletions packages/vkui/src/components/Popover/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Div } from '../Div/Div';
import { FormItem } from '../FormItem/FormItem';
import { FormLayout } from '../FormLayout/FormLayout';
import { Input } from '../Input/Input';
import { Spacing } from '../Spacing/Spacing';
import { Text } from '../Typography/Text/Text';
import { Popover, PopoverProps } from './Popover';

Expand All @@ -23,7 +24,7 @@ type Story = StoryObj<PopoverProps>;
export const Playground: Story = {
render: (args) => (
<Popover
action="hover"
trigger="hover"
placement="right"
content={
<Div>
Expand All @@ -44,7 +45,7 @@ export const Example: Story = {
return (
<>
<Popover
action="hover"
trigger="hover"
placement="right"
content={
<Div>
Expand All @@ -56,7 +57,19 @@ export const Example: Story = {
</Popover>

<Popover
action="click"
trigger="focus"
placement="right"
content={
<Div>
<Text>Привет</Text>
</Div>
}
>
<Button style={{ margin: 20 }}>focus</Button>
</Popover>

<Popover
trigger="click"
shown={shown}
onShownChange={setShown}
content={
Expand All @@ -78,6 +91,30 @@ export const Example: Story = {
>
<Button style={{ margin: '20px 0 0 0' }}>Кликни</Button>
</Popover>

<Spacing size={30} />

<Popover
trigger={['focus', 'click', 'hover']}
content={
<FormLayout>
<FormItem htmlFor="name" top="Имя">
<Input id="name" />
</FormItem>
<FormItem htmlFor="lastname" top="Фамилия">
<Input id="lastname" />
</FormItem>
<FormItem top="Соглашение">
<Checkbox name="agreement">Согласен</Checkbox>
</FormItem>
<FormItem>
<Button onClick={() => setShown(false)}>Отправить</Button>
</FormItem>
</FormLayout>
}
>
<Button style={{ margin: '20px 0 0 20px' }}>Кликни</Button>
</Popover>
</>
);
},
Expand Down
Loading

0 comments on commit 1563164

Please sign in to comment.