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

Components: Type BaseControl and VisuallyHidden #26078

Merged
merged 13 commits into from
Oct 29, 2020

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Oct 13, 2020

Description

  • Type components utils
  • Type BaseControl
  • Type VisuallyHidden

Builds on #26066
Part of #18838

How has this been tested?

Type generation completes successfully.

Types of changes

Internal / Code Quality: Add Type Checking to components.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@github-actions
Copy link

github-actions bot commented Oct 13, 2020

Size Change: 0 B

Total Size: 1.21 MB

ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.78 kB 0 B
build/api-fetch/index.js 3.45 kB 0 B
build/autop/index.js 2.84 kB 0 B
build/blob/index.js 665 B 0 B
build/block-directory/index.js 8.72 kB 0 B
build/block-directory/style-rtl.css 943 B 0 B
build/block-directory/style.css 942 B 0 B
build/block-editor/index.js 130 kB 0 B
build/block-editor/style-rtl.css 11.1 kB 0 B
build/block-editor/style.css 11.1 kB 0 B
build/block-library/editor-rtl.css 8.98 kB 0 B
build/block-library/editor.css 8.98 kB 0 B
build/block-library/index.js 146 kB 0 B
build/block-library/style-rtl.css 7.83 kB 0 B
build/block-library/style.css 7.84 kB 0 B
build/block-library/theme-rtl.css 837 B 0 B
build/block-library/theme.css 838 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 172 kB 0 B
build/components/style-rtl.css 15.2 kB 0 B
build/components/style.css 15.2 kB 0 B
build/compose/index.js 9.81 kB 0 B
build/core-data/index.js 12.3 kB 0 B
build/data-controls/index.js 772 B 0 B
build/data/index.js 8.77 kB 0 B
build/date/index.js 31.8 kB 0 B
build/deprecated/index.js 768 B 0 B
build/dom-ready/index.js 571 B 0 B
build/dom/index.js 4.46 kB 0 B
build/edit-navigation/index.js 11.2 kB 0 B
build/edit-navigation/style-rtl.css 881 B 0 B
build/edit-navigation/style.css 885 B 0 B
build/edit-post/index.js 306 kB 0 B
build/edit-post/style-rtl.css 6.38 kB 0 B
build/edit-post/style.css 6.37 kB 0 B
build/edit-site/index.js 22.1 kB 0 B
build/edit-site/style-rtl.css 3.86 kB 0 B
build/edit-site/style.css 3.85 kB 0 B
build/edit-widgets/index.js 26.4 kB 0 B
build/edit-widgets/style-rtl.css 3.1 kB 0 B
build/edit-widgets/style.css 3.1 kB 0 B
build/editor/editor-styles-rtl.css 480 B 0 B
build/editor/editor-styles.css 482 B 0 B
build/editor/index.js 43.1 kB 0 B
build/editor/style-rtl.css 3.85 kB 0 B
build/editor/style.css 3.85 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 735 B 0 B
build/format-library/index.js 7.7 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 623 B 0 B
build/i18n/index.js 3.57 kB 0 B
build/is-shallow-equal/index.js 712 B 0 B
build/keyboard-shortcuts/index.js 2.52 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.11 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.34 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.42 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.43 kB 0 B
build/priority-queue/index.js 791 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/reusable-blocks/index.js 3.06 kB 0 B
build/rich-text/index.js 13.2 kB 0 B
build/server-side-render/index.js 2.77 kB 0 B
build/shortcode/index.js 1.69 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.22 kB 0 B

compressed-size-action

@sirreal sirreal force-pushed the update/type-more-components branch 4 times, most recently from 5bd039d to 3e60029 Compare October 14, 2020 14:40
@sirreal sirreal marked this pull request as ready for review October 14, 2020 14:41
@sirreal sirreal self-assigned this Oct 14, 2020
@sirreal sirreal added [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality Needs Technical Feedback Needs testing from a developer perspective. labels Oct 14, 2020
@sirreal sirreal requested a review from ockham October 14, 2020 14:42
@@ -8,6 +8,20 @@ import classnames from 'classnames';
*/
import VisuallyHidden from '../visually-hidden';

/**
* @typedef Props
Copy link
Contributor

Choose a reason for hiding this comment

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

is this type "local"? I mean the name is very generic, is it fine?

Copy link
Member Author

@sirreal sirreal Oct 14, 2020

Choose a reason for hiding this comment

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

It is local to the module, although it is an exported type. It's pretty common practice to name types like this in my experience.

It doesn't go into any global namespace, but is accessible from another module:

/**
 * Make a local Props from the other module Props
 * @typedef {import('…').Props} Props
 */

@sirreal sirreal marked this pull request as draft October 16, 2020 08:51
@sirreal
Copy link
Member Author

sirreal commented Oct 16, 2020

I've reverted to a draft. BaseControls relies on another module after #25842 which will need to be typed.

@sirreal sirreal added [Status] In Progress Tracking issues with work in progress and removed Needs Technical Feedback Needs testing from a developer perspective. labels Oct 16, 2020
@sirreal sirreal force-pushed the update/type-more-components branch from e08d399 to 3dba833 Compare October 16, 2020 12:49
@sirreal sirreal requested a review from ItsJonQ October 16, 2020 12:58
@sirreal sirreal marked this pull request as ready for review October 16, 2020 12:58
@sirreal
Copy link
Member Author

sirreal commented Oct 16, 2020

I typed utils and this is ready for review again.

@sirreal sirreal marked this pull request as draft October 16, 2020 13:03
@sirreal
Copy link
Member Author

sirreal commented Oct 16, 2020

I seem to have made a bunch of tests fail so I'll look again 🙂

@sirreal sirreal force-pushed the update/type-more-components branch from 06bfe7d to 9732f0c Compare October 16, 2020 13:59
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Basic rendering should render with required props 1`] = `"<fieldset class=\\"block-editor-responsive-block-control\\"><legend class=\\"block-editor-responsive-block-control__title\\">Padding</legend><div class=\\"block-editor-responsive-block-control__inner\\"><div class=\\"components-base-control components-toggle-control block-editor-responsive-block-control__toggle css-wdf2ti-Wrapper e1puf3u0\\"><div class=\\"components-base-control__field css-11vcxb9-StyledField e1puf3u1\\"><span class=\\"components-form-toggle is-checked\\"><input class=\\"components-form-toggle__input\\" id=\\"inspector-toggle-control-0\\" type=\\"checkbox\\" aria-describedby=\\"inspector-toggle-control-0__help\\" checked=\\"\\"><span class=\\"components-form-toggle__track\\"></span><span class=\\"components-form-toggle__thumb\\"></span></span><label for=\\"inspector-toggle-control-0\\" class=\\"components-toggle-control__label\\">Use the same padding on all screensizes.</label></div><p id=\\"inspector-toggle-control-0__help\\" class=\\"components-base-control__help css-1wm1a55-StyledHelp e1puf3u3\\">Toggle between using the same value for all screen sizes or using a unique value per screen size.</p></div><div class=\\"block-editor-responsive-block-control__group\\"><div class=\\"components-base-control css-wdf2ti-Wrapper e1puf3u0\\"><div class=\\"components-base-control__field css-11vcxb9-StyledField e1puf3u1\\"><div class=\\"components-flex components-select-control e1cr7zh10 css-xnuudl-Flex-Root eboqfv50\\"><div class=\\"components-flex__item e1cr7zh14 css-d373l0-Item-LabelWrapper eboqfv51\\"><label for=\\"inspector-select-control-0\\" class=\\"components-input-control__label css-za86g6-Text-BaseLabel e1cr7zh13\\"><span aria-describedby=\\"rbc-desc-0\\">All</span><span class=\\"components-visually-hidden\\" id=\\"rbc-desc-0\\">Controls the padding property for All viewports.</span></label></div><div class=\\"components-input-control__container css-nj8rmx-Container e1cr7zh11\\"><select class=\\"components-select-control__input css-hwcz8s-Select e12x0a390\\" id=\\"inspector-select-control-0\\"><option value=\\"\\">Please select</option><option value=\\"small\\">Small</option><option value=\\"medium\\">Medium</option><option value=\\"large\\">Large</option></select><span class=\\"components-input-control__suffix css-1lym30p-Suffix e1cr7zh17\\"><div class=\\"css-s55r1c-DownArrowWrapper e12x0a391\\"><svg viewBox=\\"0 0 24 24\\" xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"18\\" height=\\"18\\" role=\\"img\\" aria-hidden=\\"true\\" focusable=\\"false\\"><path d=\\"M17.5 11.6L12 16l-5.5-4.4.9-1.2L12 14l4.5-3.6 1 1.2z\\"></path></svg></div></span><div aria-hidden=\\"true\\" class=\\"components-input-control__backdrop css-1s1n1to-BackdropUI e1cr7zh15\\"></div></div></div></div></div><p id=\\"all\\">All is used here for testing purposes to ensure we have access to details about the device.</p></div></div></fieldset>"`;
exports[`Basic rendering should render with required props 1`] = `"<fieldset class=\\"block-editor-responsive-block-control\\"><legend class=\\"block-editor-responsive-block-control__title\\">Padding</legend><div class=\\"block-editor-responsive-block-control__inner\\"><div class=\\"components-base-control components-toggle-control block-editor-responsive-block-control__toggle css-wdf2ti-Wrapper e1puf3u0\\"><div class=\\"components-base-control__field css-11vcxb9-StyledField e1puf3u1\\"><span class=\\"components-form-toggle is-checked\\"><input class=\\"components-form-toggle__input\\" id=\\"inspector-toggle-control-0\\" type=\\"checkbox\\" aria-describedby=\\"inspector-toggle-control-0__help\\" checked=\\"\\"><span class=\\"components-form-toggle__track\\"></span><span class=\\"components-form-toggle__thumb\\"></span></span><label for=\\"inspector-toggle-control-0\\" class=\\"components-toggle-control__label\\">Use the same padding on all screensizes.</label></div><p id=\\"inspector-toggle-control-0__help\\" class=\\"components-base-control__help css-1wm1a55-StyledHelp e1puf3u3\\">Toggle between using the same value for all screen sizes or using a unique value per screen size.</p></div><div class=\\"block-editor-responsive-block-control__group\\"><div class=\\"components-base-control css-wdf2ti-Wrapper e1puf3u0\\"><div class=\\"components-base-control__field css-11vcxb9-StyledField e1puf3u1\\"><div class=\\"components-flex components-select-control e1cr7zh10 css-xnuudl-Flex-Root eboqfv50\\"><div class=\\"components-flex__item e1cr7zh14 css-d373l0-Item-LabelWrapper eboqfv51\\"><label for=\\"inspector-select-control-0\\" class=\\"components-input-control__label css-za86g6-Text-BaseLabel e1cr7zh13\\"><span aria-describedby=\\"rbc-desc-0\\">All</span><span class=\\"components-visually-hidden\\" id=\\"rbc-desc-0\\">Controls the padding property for All viewports.</span></label></div><div class=\\"components-input-control__container css-nj8rmx-Container e1cr7zh11\\"><select class=\\"components-select-control__input css-owzv2l-Select e12x0a390\\" id=\\"inspector-select-control-0\\"><option value=\\"\\">Please select</option><option value=\\"small\\">Small</option><option value=\\"medium\\">Medium</option><option value=\\"large\\">Large</option></select><span class=\\"components-input-control__suffix css-1lym30p-Suffix e1cr7zh17\\"><div class=\\"css-s55r1c-DownArrowWrapper e12x0a391\\"><svg viewBox=\\"0 0 24 24\\" xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"18\\" height=\\"18\\" role=\\"img\\" aria-hidden=\\"true\\" focusable=\\"false\\"><path d=\\"M17.5 11.6L12 16l-5.5-4.4.9-1.2L12 14l4.5-3.6 1 1.2z\\"></path></svg></div></span><div aria-hidden=\\"true\\" class=\\"components-input-control__backdrop css-1o1vxqx-BackdropUI e1cr7zh15\\"></div></div></div></div></div><p id=\\"all\\">All is used here for testing purposes to ensure we have access to details about the device.</p></div></div></fieldset>"`;
Copy link
Member Author

Choose a reason for hiding this comment

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

The diff here is like:

  • css-hwcz8s-Select -> css-owzv2l-Select
  • css-1s1n1to-BackdropUI -> css-1o1vxqx-BackdropUI

Presumably this is because some of the files involved in calculating the styles have changed and the hash is updated. @ItsJonQ @saramarcondes does this seem alright? Is there a better way to snapshot styles like this so they're stable?

Copy link
Contributor

@sarayourfriend sarayourfriend Oct 16, 2020

Choose a reason for hiding this comment

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

Honestly a better question is whether this snapshot test is needed at all:

https://github.com/WordPress/gutenberg/blob/master/packages/block-editor/src/components/responsive-block-control/test/index.js#L69-L121

We're already testing for a bunch of assertions inside the container's markup, it's not clear to me, as someone who's only just looking at these tests, why the snapshot is valuable (especially considering we're using render rather than mount which makes the snapshot nearly impossible to sanely diff).

On the other hand, you didn't change anything about the styles, I have no idea why the class names that emotion generates wouldn't be idempotent based on the styles generated (not the module generating them). I wouldn't expect them to change if the results being passed into them didn't change but maybe @ItsJonQ will correct me about some part of emotion's infrastructure of which I am unaware.

packages/components/src/base-control/index.js Show resolved Hide resolved
Comment on lines +11 to +21
/**
* @template {keyof JSX.IntrinsicElements | import('react').JSXElementConstructor<any>} T
* @typedef OwnProps
* @property {T} [as='div'] Component to render, e.g. `"div"` or `MyComponent`.
*/

/**
* @template {keyof JSX.IntrinsicElements | import('react').JSXElementConstructor<any>} T
* @typedef {OwnProps<T> & import('react').ComponentProps<T>} Props
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! I was taking a look at this last night and having a hard time wrapping my head around how to type this. Good work!

@sirreal
Copy link
Member Author

sirreal commented Oct 22, 2020

I've rebased and fixed the conflicts, this can be reviewed.

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

One nit pick but otherwise LGTM!

* @param {any} value The incoming value.
* @param {unknown} value The incoming value.
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, why this change? I'm not fully up on when to use unknown. I guess it makes semantic sense here but what's the exact reason?

Copy link
Member Author

Choose a reason for hiding this comment

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

Glad you asked 🙂

any and unknown are both boxes that we can put anything into. The difference is that any satisfies every other type, whereas unknown is the opposite. It satisfies no other type and requires us to be careful to use it. In code that's like:

// Anything _satisfies_ both types `any` and `unknown`
var __a: any = null
var __a: any = undefined
var __a: any = ['foo']
var __a: any = 0
const a: any = new Set(['apples', 'bananas'])

var __u: unknown = null
var __u: unknown = undefined
var __u: unknown = ['foo']
var __u: unknown = 0
const u: unknown = new Set(['apples', 'bananas'])


// Type system catches this. `unknown` doesn't satisfy anything! Phew…
u.toLocaleLowerCase(); // TYPE ERROR!
// We can't call set methods either, we said we didn't know anything >:/
u.has('foo'); // TYPE ERROR!
// Typically we'll use type guards to be safe with our `unknown` types :D
u instanceof Set && u.has('apples'); // No type error 👍

// `any` type satisfies everything. It's dangerous, as if we turn off the type system completely!!
a.toLocaleLowerCase() // Oops… Set doesn't implement that! We hit s runtime error "TypeError: u.toLocaleLowerCase is not a function" 💥

This was referenced Oct 22, 2020
@sarayourfriend sarayourfriend mentioned this pull request Oct 23, 2020
6 tasks
@ItsJonQ
Copy link

ItsJonQ commented Oct 26, 2020

Haiii! I tested out a couple of the utils. A lot of this TypeScript is a bit advanced for me 😅 .
However, I'm able to get the expected Intellisense experience within VSCode as far as comments + type errors go :)

@ItsJonQ
Copy link

ItsJonQ commented Oct 28, 2020

Following up with this one! It looks like the package.json needs updating. Hopefully a rebase/merge from master branch will make the tests happier 🙏

@sirreal sirreal force-pushed the update/type-more-components branch from 69f950e to 29930cc Compare October 28, 2020 22:11
@sirreal
Copy link
Member Author

sirreal commented Oct 28, 2020

Thanks for the feedback, everyone. Rebased to fix conflicts.

@sirreal sirreal force-pushed the update/type-more-components branch from 29930cc to 279497d Compare October 29, 2020 18:50
@sirreal
Copy link
Member Author

sirreal commented Oct 29, 2020

Rebased, I think tests are back on track today and we can get this landed 🤞

@sarayourfriend sarayourfriend merged commit 4885681 into master Oct 29, 2020
@sarayourfriend sarayourfriend deleted the update/type-more-components branch October 29, 2020 22:03
@github-actions github-actions bot added this to the Gutenberg 9.3 milestone Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants