Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,8 @@ export const CalendarMonth = ({

useEffect(() => {
// Calendar month should not be focused on page load
// Datepicker should place focus in calendar month when opened
if ((shouldFocus || isDateFocused) && focusedDateValidated && focusRef.current) {
focusRef.current.focus();
} else {
setShouldFocus(true);
}
}, [focusedDate, isDateFocused, focusedDateValidated, focusRef]);

Expand Down
8 changes: 7 additions & 1 deletion packages/react-core/src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { css } from '@patternfly/react-styles';
import styles from '@patternfly/react-styles/css/components/DatePicker/date-picker';
import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
import calendarMonthStyles from '@patternfly/react-styles/css/components/CalendarMonth/calendar-month';
import { TextInput, TextInputProps } from '../TextInput/TextInput';
import { Popover, PopoverProps } from '../Popover/Popover';
import { InputGroup, InputGroupItem } from '../InputGroup';
Expand Down Expand Up @@ -214,9 +215,15 @@ const DatePickerBase = (
[setPopoverOpen, popoverOpen, selectOpen]
);

const createFocusSelectorString = (modifierClass: string) =>
`.${calendarMonthStyles.calendarMonthDatesCell}.${modifierClass} .${calendarMonthStyles.calendarMonthDate}`;
const focusSelectorForSelectedDate = createFocusSelectorString(calendarMonthStyles.modifiers.selected);
const focusSelectorForUnselectedDate = createFocusSelectorString(calendarMonthStyles.modifiers.current);

return (
<div className={css(styles.datePicker, className)} ref={datePickerWrapperRef} style={style} {...props}>
<Popover
elementToFocus={valueDate ? focusSelectorForSelectedDate : focusSelectorForUnselectedDate}
position="bottom"
bodyContent={
<CalendarMonth
Expand All @@ -232,7 +239,6 @@ const DatePickerBase = (
dayFormat={dayFormat}
weekStart={weekStart}
rangeStart={rangeStart}
isDateFocused
/>
}
showClose={false}
Expand Down
20 changes: 19 additions & 1 deletion packages/react-core/src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export interface PopoverProps {
closeBtnAriaLabel?: string;
/** Distance of the popover to its target. Defaults to 25. */
distance?: number;
/** The element to focus when the popover becomes visible. By default the first
* focusable element will receive focus.
*/
elementToFocus?: HTMLElement | SVGElement | string;
/**
* If true, tries to keep the popover in view by flipping it if necessary.
* If the position is set to 'auto', this prop is ignored.
Expand Down Expand Up @@ -269,6 +273,7 @@ export const Popover: React.FunctionComponent<PopoverProps> = ({
triggerRef,
hasNoPadding = false,
hasAutoWidth = false,
elementToFocus,
...rest
}: PopoverProps) => {
// could make this a prop in the future (true | false | 'toggle')
Expand All @@ -285,7 +290,7 @@ export const Popover: React.FunctionComponent<PopoverProps> = ({
React.useEffect(() => {
if (triggerManually) {
if (isVisible) {
show();
show(undefined, true);
} else {
hide();
}
Expand Down Expand Up @@ -404,13 +409,26 @@ export const Popover: React.FunctionComponent<PopoverProps> = ({
hide(event);
}
};

const content = (
<FocusTrap
ref={popoverRef}
active={focusTrapActive}
focusTrapOptions={{
returnFocusOnDeactivate: true,
clickOutsideDeactivates: true,
// FocusTrap's initialFocus can accept false as a value to prevent initial focus.
// We want to prevent this in case false is ever passed in.
initialFocus: elementToFocus || undefined,
checkCanFocusTrap: (containers) =>
new Promise((resolve) => {
const interval = setInterval(() => {
if (containers.every((container) => getComputedStyle(container).visibility !== 'hidden')) {
resolve();
clearInterval(interval);
}
}, 10);
}),
tabbableOptions: { displayCheck: 'none' },

fallbackFocus: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ Here the popover goes over the navigation, so the prop `appendTo` is set to the
```ts file="./PopoverAlert.tsx"

```

### Custom focus

Use the `elementToFocus` property to customize which element inside the Popover receives focus when opened.

```ts file="./PopoverCustomFocus.tsx"

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { Popover, Button } from '@patternfly/react-core';

export const PopoverCustomFocus: React.FunctionComponent = () => (
<Popover
elementToFocus="#popover-cancel-button"
showClose={false}
aria-label="Popover with custom focus"
headerContent={<div>Popover header</div>}
bodyContent={
<div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.{' '}
<Button
// Preventing default click behavior for example purposes only
onClick={(event: React.MouseEvent) => event.preventDefault()}
component="a"
isInline
variant="link"
href="#basic"
>
View the basic example
</Button>
</div>
}
footerContent={(hide) => (
<Button onClick={hide} variant="secondary" id="popover-cancel-button">
Cancel
</Button>
)}
>
<Button>Toggle popover with custom focus</Button>
</Popover>
);