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

FormFileUpload: Add lint rule for 40px size prop usage #64585

Merged
merged 3 commits into from
Aug 19, 2024
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
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,13 @@ module.exports = {
componentName +
' should have the `__next40pxDefaultSize` prop to opt-in to the new default size.',
} ) ),
{
// Falsy `__next40pxDefaultSize` without a `render` prop.
selector:
'JSXOpeningElement[name.name="FormFileUpload"]:not(:has(JSXAttribute[name.name="__next40pxDefaultSize"][value.expression.value!=false])):not(:has(JSXAttribute[name.name="render"]))',
Copy link
Member Author

Choose a reason for hiding this comment

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

Custom rule different from the others because FormFileUpload can take a render prop.

message:
'FormFileUpload should have the `__next40pxDefaultSize` prop to opt-in to the new default size.',
},
// Temporary rules until all existing components have the `__next40pxDefaultSize` prop.
...[ 'SelectControl', 'TextControl' ].map(
( componentName ) => ( {
Expand Down
19 changes: 12 additions & 7 deletions packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,22 @@ export function MediaPlaceholder( {
<>
{ renderDropZone() }
<FormFileUpload
variant="primary"
className={ clsx(
'block-editor-media-placeholder__button',
'block-editor-media-placeholder__upload-button'
render={ ( { openFileDialog } ) => (
<Button
onClick={ openFileDialog }
variant="primary"
className={ clsx(
'block-editor-media-placeholder__button',
'block-editor-media-placeholder__upload-button'
) }
>
{ __( 'Upload' ) }
</Button>
Comment on lines +494 to +504
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this to an explicit render prop, so the upsizing can be resolved along with the other Button components surrounding it.

The resulting output should remain the same. This is the render prop logic in FormFileUpload:

const ui = render ? (
render( { openFileDialog } )
) : (
<Button onClick={ openFileDialog } { ...props }>
{ children }
</Button>
);

An instance of MediaPlaceholder, with other buttons:

MediaPlaceholder

) }
onChange={ onUpload }
accept={ accept }
multiple={ !! multiple }
>
{ __( 'Upload' ) }
</FormFileUpload>
/>
{ uploadMediaLibraryButton }
{ renderUrlSelectionUI() }
{ renderFeaturedImageToggle() }
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/form-file-upload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import type Icon from '../icon';

// TODO: Replace `children` and `icon` types with props from Button once Button is typed.
export type FormFileUploadProps = {
/**
* Start opting into the larger default height that will become the default size in a future version.
*
* @default false
*/
__next40pxDefaultSize?: boolean;
Comment on lines +13 to +18
Copy link
Member Author

Choose a reason for hiding this comment

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

This was passed through as rest props to the underlying Button, but adding it here so it shows up explicitly in Storybook.

/**
* A string passed to `input` element that tells the browser which file types can be
* upload to the upload by the user use. e.g: `image/*,video/*`.
Expand Down
Loading