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

Fix React forwardRef warnings for TooltipAnchors #54492

Merged
merged 4 commits into from
Sep 18, 2023
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
4 changes: 4 additions & 0 deletions packages/icons/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- `<Icon>` now forwards ref to the underlying child component ([#54492](https://github.com/WordPress/gutenberg/pull/54492)).

## 9.32.0 (2023-08-31)

### Bug Fix
Expand Down
14 changes: 8 additions & 6 deletions packages/icons/src/icon/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/**
* WordPress dependencies
*/
import { cloneElement } from '@wordpress/element';
import { cloneElement, forwardRef } from '@wordpress/element';

/** @typedef {{icon: JSX.Element, size?: number} & import('@wordpress/primitives').SVGProps} IconProps */

/**
* Return an SVG icon.
*
* @param {IconProps} props icon is the SVG component to render
* size is a number specifiying the icon size in pixels
* Other props will be passed to wrapped SVG component
* @param {IconProps} props icon is the SVG component to render
* size is a number specifiying the icon size in pixels
* Other props will be passed to wrapped SVG component
* @param {import('react').ForwardedRef<HTMLElement>} ref The forwarded ref to the SVG element.
*
* @return {JSX.Element} Icon component
*/
function Icon( { icon, size = 24, ...props } ) {
function Icon( { icon, size = 24, ...props }, ref ) {
return cloneElement( icon, {
width: size,
height: size,
...props,
ref,
} );
}

export default Icon;
export default forwardRef( Icon );
4 changes: 4 additions & 0 deletions packages/primitives/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- `<SVG>` now forwards ref to the underlying `<svg>` element ([#54492](https://github.com/WordPress/gutenberg/pull/54492)).

## 3.39.0 (2023-08-31)

## 3.38.0 (2023-08-16)
Expand Down
44 changes: 24 additions & 20 deletions packages/primitives/src/svg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createElement } from '@wordpress/element';
import { createElement, forwardRef } from '@wordpress/element';

/** @typedef {{isPressed?: boolean} & import('react').ComponentPropsWithoutRef<'svg'>} SVGProps */

Expand Down Expand Up @@ -82,23 +82,27 @@ export const LinearGradient = ( props ) =>
*/
export const Stop = ( props ) => createElement( 'stop', props );

/**
*
* @param {SVGProps} props isPressed indicates whether the SVG should appear as pressed.
* Other props will be passed through to svg component.
*
* @return {JSX.Element} Stop component
*/
export const SVG = ( { className, isPressed, ...props } ) => {
const appliedProps = {
...props,
className:
classnames( className, { 'is-pressed': isPressed } ) || undefined,
'aria-hidden': true,
focusable: false,
};
export const SVG = forwardRef(
/**
* @param {SVGProps} props isPressed indicates whether the SVG should appear as pressed.
* Other props will be passed through to svg component.
* @param {import('react').ForwardedRef<SVGSVGElement>} ref The forwarded ref to the SVG element.
*
* @return {JSX.Element} Stop component
*/
( { className, isPressed, ...props }, ref ) => {
const appliedProps = {
...props,
className:
classnames( className, { 'is-pressed': isPressed } ) ||
undefined,
'aria-hidden': true,
focusable: false,
};

// Disable reason: We need to have a way to render HTML tag for web.
// eslint-disable-next-line react/forbid-elements
return <svg { ...appliedProps } />;
};
// Disable reason: We need to have a way to render HTML tag for web.
// eslint-disable-next-line react/forbid-elements
return <svg { ...appliedProps } ref={ ref } />;
}
);
SVG.displayName = 'SVG';
Loading