Skip to content

Add an eslint rule to use cross-environment SVG primitives#10652

Merged
gziolo merged 4 commits intomasterfrom
try/removing-svg-elements
Oct 25, 2018
Merged

Add an eslint rule to use cross-environment SVG primitives#10652
gziolo merged 4 commits intomasterfrom
try/removing-svg-elements

Conversation

@youknowriad
Copy link
Copy Markdown
Contributor

Still WIP, the idea is to try to force the usage of the SVG primitives instead of the raw components to ensure mobile compatibility.

  • I don't like that this is forcing us to add a dependency: blocks -> components. Makes me think these primitives should be better in the element package.
  • The eslint rule is too aggressive at the moment, it also catches props named "path"

@youknowriad youknowriad added the [Type] Code Quality Issues or PRs that relate to code quality label Oct 16, 2018
@youknowriad youknowriad requested review from aduth and gziolo October 16, 2018 15:34
.eslintrc.js Outdated
message: 'Deprecated functions must be removed before releasing this version.',
},
{
selector: 'JSXIdentifier[name=/^(path|svg)$/]',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we also add <g> and <polygon> that we already have componentized as well?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, sure.

The eslint rule is too aggressive at the moment, it also catches props named "path"

As Riad mentioned, this rule needs to be updated anyways.

@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 17, 2018

Many thanks for opening this PR @youknowriad ❤️

@gziolo gziolo added this to the 4.2 - API freeze milestone Oct 17, 2018
@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 17, 2018

I don't like that this is forcing us to add a dependency: blocks -> components. Makes me think these primitives should be better in the element package.

<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><Path d="M19 7h-1V5h-4v2h-4V5H6v2H5c-1.1 0-2 .9-2 2v10h18V9c0-1.1-.9-2-2-2zm0 10H5V9h14v8z" /></SVG> 

I personally think that @wordpress/blocks shouldn't be so opinionated about the default icon. If we could register a default icon instead it would solve the issue. It could be similar to:

setDefaultBlockName( paragraph.name );
if ( window.wp && window.wp.oldEditor ) {
	setFreeformContentHandlerName( classic.name );
}
setUnregisteredTypeHandlerName( missing.name );
...
setDefaultIcon( 
    <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><Path d="M19 7h-1V5h-4v2h-4V5H6v2H5c-1.1 0-2 .9-2 2v10h18V9c0-1.1-.9-2-2-2zm0 10H5V9h14v8z" /></SVG>
);

@gziolo gziolo mentioned this pull request Oct 23, 2018
4 tasks
@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 23, 2018

@Tug if you have some time, you can help to land this one.

@gziolo gziolo added Mobile Web Viewport sizes for mobile and tablet devices Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) and removed Mobile Web Viewport sizes for mobile and tablet devices labels Oct 23, 2018
@gziolo gziolo force-pushed the try/removing-svg-elements branch from 37533b2 to 7f8df8c Compare October 23, 2018 10:07
@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 23, 2018

This should be ready to ship. I figured out that there is an existing rule in react linting library which we could take advantage of: https://github.com/yannickcr/eslint-plugin-react/blob/HEAD/docs/rules/forbid-elements.md.

@gziolo gziolo requested review from a team and mzorz October 23, 2018 10:10
aduth
aduth previously requested changes Oct 23, 2018
Copy link
Copy Markdown
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

This white-screens for me (and apparently in Travis) with the following error:

Uncaught TypeError: Cannot read property 'SVG' of undefined

Maybe missing a dependency? (From blocks to components)

@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 24, 2018

Maybe missing a dependency? (From blocks to components)

Yes, it is an actual issue which @youknowriad raised earlier. I will refactor code to avoid using SVG inside @wordpress/blocks package. It should be UI agnostic.

@gziolo gziolo force-pushed the try/removing-svg-elements branch from 7f8df8c to 6efe432 Compare October 24, 2018 08:29

function renderIcon( icon ) {
if ( 'string' === typeof icon ) {
if ( icon === 'block-default' ) {
Copy link
Copy Markdown
Member

@gziolo gziolo Oct 24, 2018

Choose a reason for hiding this comment

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

@jasmussen - is it okay to handle SVG replacement in here?

We want to avoid having a dependency on @wordpress/components inside @wordpress/blocks. My reasoning is as follows: blocks API should be UI agnostic as much as possible, that's why it provides only an icon name. BlockIcon is always used to render an actual icon, so we can be more opinionated here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you elaborate a little bit? Not sure I understand the context.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See also my previous comment: #10652 (comment).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I changed my mind when entertaining with the exisitng implementation. I reverted all change in normalizeIconObject to use default-block dashicon. However, it is replaced with the new nice SVG icon inside BlockEdit. This should behave exactly the same in the UI.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, gotcha. I don't have strong opinions here, and I would defer to you two for the technical implementation. What matters is:

  • We encourage the use of SVG (i.e. make it easy)
  • We can provide an icon set even for new blocks, like Dashicons, in the future.
  • 3rd party provided SVGs are processed so role and focusable attributes are added.

If those values are upheld, 👍 👍 from me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can provide an icon set even for new blocks, like Dashicons, in the future.

I think it would be convenient to be able to use strings if someone is happy with the default icon set.

3rd party provided SVGs are processed so role and focusable attributes are added.

This is what SVG component does :)

@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 24, 2018

I don't like that this is forcing us to add a dependency: blocks -> components. Makes me think these primitives should be better in the element package.

It's no longer the case, see #10652 (comment).

This white-screens for me (and apparently in Travis) with the following error:

Uncaught TypeError: Cannot read property 'SVG' of undefined

Maybe missing a dependency? (From blocks to components)

e2e tests pass so I think block icons should be fine as @jorgefilipecosta build an extensive set of assertions for them. It was missing dependency, but I solved it by making @wordpress/blocks components agnostic.

@gziolo gziolo requested a review from aduth October 24, 2018 08:55
@gziolo
Copy link
Copy Markdown
Member

gziolo commented Oct 24, 2018

I added fbd3a30 to ensure that fix for the regression (#10987) works with the default SVG icon, too.

Copy link
Copy Markdown
Contributor

@mzorz mzorz left a comment

Choose a reason for hiding this comment

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

great work @gziolo - there's a conflict to be resolved in packages/editor/src/components/block-icon/index.js but otherwise LGTM after that one gets fixed

@aduth aduth dismissed their stale review October 24, 2018 12:37

Error addressed

youknowriad and others added 2 commits October 25, 2018 07:55
Uses an existing react eslint rule to perform validation.
Adds new components to make the existing icons mobile app friendly.
@gziolo gziolo force-pushed the try/removing-svg-elements branch from fbd3a30 to 0b37479 Compare October 25, 2018 06:01
@gziolo gziolo merged commit 578d0ef into master Oct 25, 2018
@gziolo gziolo deleted the try/removing-svg-elements branch October 25, 2018 09:04
antpb pushed a commit to antpb/gutenberg that referenced this pull request Oct 26, 2018
…#10652)

* Add an eslint rule to use cross-environment SVG primitives

* Extend the list of forbidden elements
Uses an existing react eslint rule to perform validation.
Adds new components to make the existing icons mobile app friendly.

* Change the way the default icon for block is set

* Ensure that default icon goes through the process of width and height normalization in BlockIcon
Props to @jasmussen for helping to catch it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants