Skip to content

Commit

Permalink
[@mantine/core] Popover: Add option to configure focus returning logi…
Browse files Browse the repository at this point in the history
…c with returnFocus prop
  • Loading branch information
rtivital committed Oct 5, 2022
1 parent 0de019f commit e6943ff
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/mantine-core/src/Popover/Popover.context.ts
Expand Up @@ -37,6 +37,7 @@ interface PopoverContext {
withRoles: boolean;
targetProps: Record<string, any>;
disabled: boolean;
returnFocus: boolean;
}

export const [PopoverContextProvider, usePopoverContext] = createSafeContext<PopoverContext>(
Expand Down
8 changes: 3 additions & 5 deletions src/mantine-core/src/Popover/Popover.story.tsx
Expand Up @@ -49,7 +49,7 @@ export function WithArrow() {
);
}

export function Usage() {
export function Controlled() {
const [opened, setState] = useState(false);

return (
Expand All @@ -61,17 +61,15 @@ export function Usage() {
position="bottom"
withArrow
trapFocus
width={300}
radius="md"
returnFocus
>
<Popover.Target>
<Button onClick={() => setState((c) => !c)}>Toggle popover</Button>
</Popover.Target>

<Popover.Dropdown>
<input />
<input data-autofocus />
<input />
<Button onClick={() => setState(false)}>Close</Button>
</Popover.Dropdown>
</Popover>
</div>
Expand Down
6 changes: 6 additions & 0 deletions src/mantine-core/src/Popover/Popover.tsx
Expand Up @@ -82,6 +82,9 @@ export interface PopoverBaseProps {

/** If set, popover dropdown will not render */
disabled?: boolean;

/** Determines whether focus should be automatically returned to control when dropdown closes, false by default */
returnFocus?: boolean;
}

export interface PopoverProps extends PopoverBaseProps {
Expand Down Expand Up @@ -135,6 +138,7 @@ const defaultProps: Partial<PopoverProps> = {
closeOnEscape: true,
trapFocus: false,
withRoles: true,
returnFocus: false,
clickOutsideEvents: ['mousedown', 'touchstart'],
zIndex: getDefaultZIndex('popover'),
__staticSelector: 'Popover',
Expand Down Expand Up @@ -176,6 +180,7 @@ export function Popover(props: PopoverProps) {
__staticSelector,
withRoles,
disabled,
returnFocus,
...others
} = useComponentDefaultProps('Popover', defaultProps, props);

Expand Down Expand Up @@ -213,6 +218,7 @@ export function Popover(props: PopoverProps) {
>
<PopoverContextProvider
value={{
returnFocus,
disabled,
controlled: popover.controlled,
reference: (node) => {
Expand Down
Expand Up @@ -31,7 +31,7 @@ export function PopoverDropdown({

const returnFocus = useFocusReturn({
opened: ctx.opened,
shouldReturnFocus: false,
shouldReturnFocus: ctx.returnFocus,
});

const accessibleProps = ctx.withRoles
Expand Down

0 comments on commit e6943ff

Please sign in to comment.