Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiModal] Wrapping header in H1 if a string is returned #5494

Merged
merged 5 commits into from
Dec 21, 2021
Merged

[EuiModal] Wrapping header in H1 if a string is returned #5494

merged 5 commits into from
Dec 21, 2021

Conversation

1Copenut
Copy link
Contributor

@1Copenut 1Copenut commented Dec 20, 2021

Summary

Add a check in the modal_header_title.tsx file to wrap props.children in an H1 if a string is passed into the component. Anything else (not a string) is ignored by this check. Closes #5276.

Checklist

  • Check against all themes for compatibility in both light and dark modes
  • Checked in mobile
  • Checked in Chrome, Safari, Edge, and Firefox
  • Props have proper autodocs and playground toggles
  • Added documentation
  • Checked Code Sandbox works for any docs examples
  • Added or updated jest and cypress tests
  • Checked for breaking changes and labeled appropriately
  • Checked for accessibility including keyboard-only and screenreader modes
  • A changelog entry exists and is marked appropriately

Comment on lines 23 to 30
const renderChildren = () =>
React.Children.map(children, (child) => {
if (typeof child === 'string') return <h1>{child}</h1>;
return child;
});
return (
<div className={classes} {...rest}>
{children}
{renderChildren()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% on the typeof children, but I think this should be more robust. I'd try to avoid maping children if possible in the edge case that someone passes an array/map of strings, in which case you'd end up with multiple <h1>s (another accessibility violation)

Suggested change
const renderChildren = () =>
React.Children.map(children, (child) => {
if (typeof child === 'string') return <h1>{child}</h1>;
return child;
});
return (
<div className={classes} {...rest}>
{children}
{renderChildren()}
const childrenWithHeading = useMemo(() =>
if (typeof children === 'string') return <h1>{children}</h1>;
return children;
}, [children]);
return (
<div className={classes} {...rest}>
{childrenWithHeading}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call @constancecchen. I refactored using your recommended approach, and found what I think is a better way to reason about children being a string than typeof. Source article here: https://overreacted.io/why-do-react-elements-have-typeof-property/

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5494/

@1Copenut 1Copenut requested a review from cee-chen December 20, 2021 23:13
@1Copenut 1Copenut marked this pull request as ready for review December 20, 2021 23:13
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5494/

Comment on lines 23 to 32
const childrenWithHeading = useMemo(() => {
if (children && !children.hasOwnProperty('$$typeof'))
return <h1>{children}</h1>;
return children;
}, [children]);
return (
<div className={classes} {...rest}>
{children}
{childrenWithHeading}
</div>
);
Copy link
Contributor

@cee-chen cee-chen Dec 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(continuing convo from #5494 (comment))

Ahh actually I totally forgot about React.isValidElement (https://davidwalsh.name/react-isvalidelement)! I think it's a bit more readable and exactly what we're looking for (distinguishing JSX vs strings):

Suggested change
const childrenWithHeading = useMemo(() => {
if (children && !children.hasOwnProperty('$$typeof'))
return <h1>{children}</h1>;
return children;
}, [children]);
return (
<div className={classes} {...rest}>
{children}
{childrenWithHeading}
</div>
);
return (
<div className={classes} {...rest}>
{React.isValidElement(children) ? children : <h1>{children}</h1>}
</div>
);

LMK what you think!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a lot cleaner, and is the method I was hoping for, but not finding. Works great, and is easy to reason about.

@1Copenut 1Copenut requested a review from cee-chen December 21, 2021 15:48
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5494/

Copy link
Contributor

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Love it!! Thanks for the awesome accessibility improvements!

Copy link
Contributor

@thompsongl thompsongl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after the changelog update

CHANGELOG.md Outdated
@@ -16,6 +16,7 @@
- Fixed an accessibility issue where `EuiDatePicker` time options did not have unique IDs ([#5466](https://github.com/elastic/eui/pull/5466))
- Fixed global and reset styles when using the legacy theme ([#5473](https://github.com/elastic/eui/pull/5473))
- Fixed `EuiSuperDatePicker` not passing `isDisabled` to `EuiAutoRefresh` ([#5472](https://github.com/elastic/eui/pull/5472))
- Fixed `EuiModalHeaderTitle` to conditionally wrap title strings in an H1 ([#5494](https://github.com/elastic/eui/pull/5494))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move up to main

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @thompsongl. I'll make this change and merge when tests pass.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget the "Enable auto-merge" option also! It'll auto merge for ya when tests pass

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5494/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[EuiConfirmModal] Title should be wrapped in an HTML heading if provided as a string
4 participants