Skip to content

Commit

Permalink
get rid of suppressHydrationWarning
Browse files Browse the repository at this point in the history
  • Loading branch information
krzotki committed Jul 29, 2024
1 parent 1218c69 commit 0ff67fe
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 38 deletions.
7 changes: 2 additions & 5 deletions src/components/card-interactive/CardCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as React from 'react';
import cx from 'classnames';
import Checkbox from '../form-elements/checkbox/Checkbox';
import generateRandomString from '../../js/generateRandomString';

export interface CardCheckboxPropsType
extends Omit<React.ComponentPropsWithoutRef<'label'>, 'onChange'> {
/**
Expand Down Expand Up @@ -160,7 +158,8 @@ export const CardCheckboxRoot = React.forwardRef<
isControlled ? checked : defaultChecked
);

const cardId = React.useMemo(() => id || generateRandomString(), [id]);
const genId = React.useId();
const cardId = id || genId;

const cssVariables = {
'--card-width': width,
Expand Down Expand Up @@ -205,7 +204,6 @@ export const CardCheckboxRoot = React.forwardRef<
// On iOS the :active pseudo state is triggered only when there is a touch event set on the HTML element
// and we use active pseudo class to provide press feedback.
onTouchStart={() => null}
suppressHydrationWarning
{...props}
>
<input
Expand All @@ -223,7 +221,6 @@ export const CardCheckboxRoot = React.forwardRef<
aria-invalid={invalid ? true : undefined}
aria-labelledby={ariaLabelledBy || `label-${cardId}`}
aria-describedby={ariaDescribedBy}
suppressHydrationWarning
/>
<div className="sg-card-interactive__border">
<div className="sg-card-interactive__background">{children}</div>
Expand Down
6 changes: 2 additions & 4 deletions src/components/card-interactive/CardRadio.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import cx from 'classnames';
import Radio from '../form-elements/radio/Radio';
import generateRandomString from '../../js/generateRandomString';
import {useCardRadioGroupContext} from './CardRadioGroupContext';
import type {StyleType} from './types';

Expand Down Expand Up @@ -128,7 +127,8 @@ const CardRadio = React.forwardRef<HTMLInputElement, CardRadioPropsType>(
) => {
const context = useCardRadioGroupContext();

const cardId = React.useMemo(() => id || generateRandomString(), [id]);
const genId = React.useId();
const cardId = id || genId;
const isChecked = context.value === value;
const isRequired = context.required || required;
const isDisabled = context.disabled || disabled;
Expand Down Expand Up @@ -169,7 +169,6 @@ const CardRadio = React.forwardRef<HTMLInputElement, CardRadioPropsType>(
// On iOS the :active pseudo state is triggered only when there is a touch event set on the HTML element
// and we use active pseudo class to provide press feedback.
onTouchStart={() => null}
suppressHydrationWarning
{...props}
>
<input
Expand All @@ -186,7 +185,6 @@ const CardRadio = React.forwardRef<HTMLInputElement, CardRadioPropsType>(
aria-invalid={isInvalid}
aria-labelledby={ariaLabelledBy || `label-${cardId}`}
aria-describedby={ariaDescribedBy}
suppressHydrationWarning
/>
<div className="sg-card-interactive__border">
<div className="sg-card-interactive__background">{children}</div>
Expand Down
13 changes: 4 additions & 9 deletions src/components/icons/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import classNames from 'classnames';
import {generateId} from '../utils';

export type IconTypeType =
| 'academic_cap'
Expand Down Expand Up @@ -547,10 +546,6 @@ export type IconPropsType =
| 'description'
>);

function generateIdSuffix(type: string) {
return `${type}-${generateId()}`;
}

const Icon = ({
color = ICON_COLOR['icon-white'],
size = 24,
Expand All @@ -572,7 +567,9 @@ const Icon = ({
);
const iconType = `#icon-${type}`;
const Tag = as;
const idSuffix = generateIdSuffix(type);

const id = React.useId();
const idSuffix = `${type}-${id}`;
const titleId = `title-${idSuffix}`;
const defaultTitle = String(type).replace(/_/g, ' ');
const descId = `desc-${idSuffix}`;
Expand All @@ -581,7 +578,6 @@ const Icon = ({
? undefined
: [title, description].filter(Boolean).join(', ');

// suppressHydrationWarning is used until 'useId' hook is available
return (
<Tag {...props} className={iconClass} aria-label={ariaLabel}>
{type ? (
Expand All @@ -590,9 +586,8 @@ const Icon = ({
role="img"
aria-labelledby={labelledBy}
focusable="false"
suppressHydrationWarning
>
<text id={titleId} visibility="hidden" suppressHydrationWarning>
<text id={titleId} visibility="hidden">
{title || defaultTitle}
</text>
{description && <desc id={descId}>{description}</desc>}
Expand Down
12 changes: 6 additions & 6 deletions src/components/select-menu/SelectMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
useMergeRefs,
} from '@floating-ui/react';

import {isTouchScreen, __DEV__, invariant, generateId} from '../utils';
import {isTouchScreen, __DEV__, invariant} from '../utils';
import Icon from '../icons/Icon';
import SubjectIcon from '../subject-icons/SubjectIcon';
import type {IconTypeType as SubjectIconTypeType} from '../subject-icons/SubjectIcon';
Expand Down Expand Up @@ -208,15 +208,17 @@ const SelectMenu = React.forwardRef<HTMLDivElement, SelectMenuPropsType>(
...additionalProps
} = props;

const {current: id} = React.useRef<string>(`select-${generateId()}`);
const id = React.useId()

const selectId = `select-${id}`

if (__DEV__) {
invariant(
!(valid && invalid),
`Select cannot be valid and invalid at the same time.`
);
}
const wrapperId = `${id}-wrapper`;
const wrapperId = `${selectId}-wrapper`;
const popupClassName = 'sg-select-menu__popup';
const popupContentClassName = 'sg-select-menu__options-wrapper';
const selectElementClassName = 'sg-select-menu__element';
Expand Down Expand Up @@ -415,11 +417,10 @@ const SelectMenu = React.forwardRef<HTMLDivElement, SelectMenuPropsType>(
id={wrapperId}
className={selectClass}
onClick={onClick}
suppressHydrationWarning
>
<div
ref={selectRef}
id={id}
id={selectId}
className={selectElementClassName}
role="combobox"
tabIndex={!disabled ? 0 : -1}
Expand All @@ -429,7 +430,6 @@ const SelectMenu = React.forwardRef<HTMLDivElement, SelectMenuPropsType>(
aria-expanded={isExpanded}
aria-haspopup="listbox"
data-status={status}
suppressHydrationWarning
{...interactions.getReferenceProps({
// Handle keyboard
onKeyDown(event) {
Expand Down
6 changes: 2 additions & 4 deletions src/components/subject-icons/SubjectIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react';
import classNames from 'classnames';
import type {IconColorType} from '../icons/Icon';
import {ICON_COLOR} from '../icons/Icon';
import {generateId} from '../utils';

export type IconTypeType =
| 'accountancy'
Expand Down Expand Up @@ -185,7 +184,7 @@ const SubjectIcon = ({
title,
...props
}: SubjectIconPropsType) => {
const {current: id} = React.useRef(generateId());
const id = React.useId()
const iconClass = classNames(
'sg-subject-icon',
{
Expand All @@ -205,9 +204,8 @@ const SubjectIcon = ({
className={iconClass}
aria-labelledby={titleId}
role="img"
suppressHydrationWarning
>
<text id={titleId} visibility="hidden" suppressHydrationWarning>
<text id={titleId} visibility="hidden">
{title || defaultTitle}
</text>
<use xlinkHref={iconType} aria-hidden="true" />
Expand Down
13 changes: 3 additions & 10 deletions src/components/text/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import classNames from 'classnames';
import {__DEV__, invariant, generateId} from '../utils';
import {__DEV__, invariant} from '../utils';
import Text from './Text';
import type {TextColorType, TextSizeType} from './Text';
import {
Expand Down Expand Up @@ -111,7 +111,7 @@ const Link = (props: LinkPropsType) => {
hideNewTabIndicator = false,
...additionalProps
} = props;
const {current: labelId} = React.useRef(generateId());
const labelId = React.useId();
let textSize: ResponsivePropType<TextSizeType> | undefined;

if (typeof size === 'object') {
Expand Down Expand Up @@ -163,7 +163,6 @@ const Link = (props: LinkPropsType) => {
className
);

// suppressHydrationWarning is used until 'useId' hook is available
if (as === 'button') {
return (
<Text
Expand All @@ -172,12 +171,7 @@ const Link = (props: LinkPropsType) => {
className={linkClass}
size={textSize}
>
<span
id={labelId}
aria-label={ariaLabel}
aria-hidden
suppressHydrationWarning
>
<span id={labelId} aria-label={ariaLabel} aria-hidden>
{children}
</span>
<button
Expand All @@ -186,7 +180,6 @@ const Link = (props: LinkPropsType) => {
disabled={disabled}
type="button"
aria-labelledby={labelId}
suppressHydrationWarning
/>
</Text>
);
Expand Down

0 comments on commit 0ff67fe

Please sign in to comment.