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

Primitives: Add types #21482

Merged
merged 11 commits into from
Apr 10, 2020
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@storybook/addon-viewport": "5.3.2",
"@storybook/react": "5.3.2",
"@testing-library/react": "10.0.2",
"@types/classnames": "2.2.10",
"@types/lodash": "4.14.149",
"@types/prettier": "1.19.0",
"@types/qs": "6.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ exports[`ColorPaletteControl matches the snapshot 1`] = `
type="button"
/>
<svg
aria-hidden="true"
aria-hidden={true}
fill="#000000"
focusable="false"
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ exports[`ColorPalette should render a dynamic toolbar of colors 1`] = `
xmlns="http://www.w3.org/2000/svg"
>
<svg
aria-hidden="true"
aria-hidden={true}
fill="#000000"
focusable="false"
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ exports[`ColorPicker should commit changes to all views on blur 1`] = `
type="button"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down Expand Up @@ -329,8 +329,8 @@ exports[`ColorPicker should commit changes to all views on keyDown = DOWN 1`] =
type="button"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down Expand Up @@ -503,8 +503,8 @@ exports[`ColorPicker should commit changes to all views on keyDown = ENTER 1`] =
type="button"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down Expand Up @@ -677,8 +677,8 @@ exports[`ColorPicker should commit changes to all views on keyDown = UP 1`] = `
type="button"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down Expand Up @@ -851,8 +851,8 @@ exports[`ColorPicker should only update input view for draft changes 1`] = `
type="button"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down Expand Up @@ -1025,8 +1025,8 @@ exports[`ColorPicker should render color picker 1`] = `
type="button"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ exports[`MoreMenu should match snapshot 1`] = `
xmlns="http://www.w3.org/2000/svg"
>
<svg
aria-hidden="true"
focusable="false"
aria-hidden={true}
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ exports[`PluginMoreMenuItem renders menu item as button properly 1`] = `
type="button"
>
<svg
aria-hidden="true"
aria-hidden={true}
className="components-menu-items__item-icon"
focusable="false"
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ exports[`EnableCustomFieldsOption renders a checked checkbox and a confirmation
value="1"
/>
<svg
aria-hidden="true"
aria-hidden={true}
className="components-checkbox-control__checked"
focusable="false"
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down Expand Up @@ -80,9 +80,9 @@ exports[`EnableCustomFieldsOption renders a checked checkbox when custom fields
value="1"
/>
<svg
aria-hidden="true"
aria-hidden={true}
className="components-checkbox-control__checked"
focusable="false"
focusable={false}
height={24}
role="img"
viewBox="0 0 24 24"
Expand Down
4 changes: 4 additions & 0 deletions packages/primitives/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Master

Include TypeScript type declarations ([#21482](https://github.com/WordPress/gutenberg/pull/21482))

## 1.0.0

Initial release.
1 change: 1 addition & 0 deletions packages/primitives/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"module": "build-module/index.js",
"react-native": "src/index",
"sideEffects": false,
"types": "build-types",
"dependencies": {
"@babel/runtime": "^7.9.2",
"@wordpress/element": "file:../element",
Expand Down
69 changes: 67 additions & 2 deletions packages/primitives/src/svg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,91 @@ import classnames from 'classnames';
*/
import { createElement } from '@wordpress/element';

// Disable reason: JSDoc linter doesn't seem to parse the union (`&`) correctly.
/* eslint-disable jsdoc/valid-types */
/** @typedef {{isPressed: boolean} & import('react').ComponentPropsWithoutRef<'svg'>} SVGProps */
/* eslint-enable jsdoc/valid-types */

/**
* @param {import('react').ComponentPropsWithoutRef<'circle'>} props
*
* @return {JSX.Element} Circle component
*/
export const Circle = ( props ) => createElement( 'circle', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'g'>} props
*
* @return {JSX.Element} G component
*/
export const G = ( props ) => createElement( 'g', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'path'>} props
*
* @return {JSX.Element} Path component
*/
export const Path = ( props ) => createElement( 'path', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'polygon'>} props
*
* @return {JSX.Element} Polygon component
*/
export const Polygon = ( props ) => createElement( 'polygon', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'rect'>} props
*
* @return {JSX.Element} Rect component
*/
export const Rect = ( props ) => createElement( 'rect', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'defs'>} props
*
* @return {JSX.Element} Defs component
*/
export const Defs = ( props ) => createElement( 'defs', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'radialGradient'>} props
*
* @return {JSX.Element} RadialGradient component
*/
export const RadialGradient = ( props ) =>
createElement( 'radialGradient', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'linearGradient'>} props
*
* @return {JSX.Element} LinearGradient component
*/
export const LinearGradient = ( props ) =>
createElement( 'linearGradient', props );

/**
* @param {import('react').ComponentPropsWithoutRef<'stop'>} props
*
* @return {JSX.Element} Stop component
*/
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,
role: 'img',
'aria-hidden': 'true',
focusable: 'false',
'aria-hidden': true,
focusable: false,
Comment on lines +94 to +95
Copy link
Member

@sirreal sirreal Apr 10, 2020

Choose a reason for hiding this comment

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

The React declarations require this change, these props are typed as booleans. I checked in a code sandbox and the following confirms this should produce the same HTML.

This JSX:

<>
      <svg aria-hidden="true" focusable="false" />
      <svg aria-hidden={ true } focusable={ false } />
</>

Produces this HTML:

<svg aria-hidden="true" focusable="false"></svg>
<svg aria-hidden="true" focusable="false"></svg>

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 for checking!

};

// Disable reason: We need to have a way to render HTML tag for web.
Expand Down
9 changes: 9 additions & 0 deletions packages/primitives/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "build-types"
},
"include": [ "src/**/*" ],
"references": [ { "path": "../element" } ]
Copy link
Member

Choose a reason for hiding this comment

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

That's the only project reference we need 👍

"@babel/runtime": "^7.9.2",
"@wordpress/element": "file:../element",
"classnames": "^2.2.5"

}
Loading