Skip to content

Commit

Permalink
[EuiDatePickerRange] Use container queries for better inline respon…
Browse files Browse the repository at this point in the history
…siveness

(sadly, jsdom errors on this, so we have to add another console error exclusion)
  • Loading branch information
cee-chen committed May 23, 2023
1 parent b340607 commit df16cc7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scripts/jest/setup/throw_on_console_error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { format } from 'util'
import { format } from 'util';

// Fail if a test ends up `console.error`-ing, e.g. if React logs because of a
// failed prop types check.
Expand All @@ -16,5 +16,15 @@ console.error = (message, ...rest) => {
return;
}

// @see https://github.com/jsdom/jsdom/issues/2177
// JSDOM doesn't yet know how to parse @container CSS queries -
// all we can do is silence its errors for now
if (
typeof message === 'string' &&
message.startsWith('Error: Could not parse CSS stylesheet')
) {
return;
}

throw new Error(format(message, ...rest));
};
27 changes: 27 additions & 0 deletions src/components/date_picker/date_picker_range.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ export const euiDatePickerRangeInlineStyles = (
) => {
const { euiTheme } = euiThemeContext;

// Use a container query to stack date pickers vertically if the container is
// not wide enough to fit both. We need a fn for this to render two width queries,
// depending on whether time selection is being rendered or not
const containerQuery = (containerMaxWidth: number) => `
display: block;
container-type: inline-size;
.euiFormControlLayout__childrenWrapper {
// Use static px widths for now, since render behavior comes from a third party library
@container (max-width: ${containerMaxWidth * 2}px) {
// Unset grid display
display: block !important;
// Center and point the default delimiter arrow downwards
& > .euiText > [data-icon-type='sortRight'] {
transform: rotate(90deg);
margin-inline: auto;
}
}
}`;

return {
inline: css`
.euiFormControlLayoutDelimited {
Expand Down Expand Up @@ -75,6 +96,12 @@ export const euiDatePickerRangeInlineStyles = (
padding: 0;
}
`,
responsive: css`
${containerQuery(275)}
`,
responsiveWithTimeSelect: css`
${containerQuery(350)}
`,
shadow: css`
.euiFormControlLayoutDelimited {
${euiShadowMedium(euiThemeContext)}
Expand Down
10 changes: 10 additions & 0 deletions src/components/date_picker/date_picker_range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,18 @@ export const EuiDatePickerRange: FunctionComponent<EuiDatePickerRangeProps> = ({
const cssStyles = [styles.euiDatePickerRange];

if (inline) {
// Determine the inline container query to use based on the width of the react-datepicker
const hasTimeSelect =
startDateControl.props.showTimeSelect ||
endDateControl.props.showTimeSelect;

const inlineStyles = euiDatePickerRangeInlineStyles(euiTheme);
cssStyles.push(inlineStyles.inline);
cssStyles.push(
hasTimeSelect
? inlineStyles.responsiveWithTimeSelect
: inlineStyles.responsive
);
if (shadow) cssStyles.push(inlineStyles.shadow);
}

Expand Down

0 comments on commit df16cc7

Please sign in to comment.