-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ResponsiveWrapper: Convert to TypeScript #46480
Conversation
Size Change: +27 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
const [ containerResizeListener, { width: containerWidth } ] = | ||
useResizeObserver(); | ||
if ( Children.count( children ) !== 1 ) { | ||
return null; | ||
} | ||
const imageStyle = { | ||
paddingBottom: | ||
naturalWidth < containerWidth | ||
naturalWidth < ( containerWidth ?? 0 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TS was not happy because containerWidth
can potentially be null
. (Apparently useResizeObserver()
will return null for this value until after the first render.)
My code change here just makes explicit what was happening implicitly — a null
will be interpreted as 0
when compared with the <
operator. This maintains the current behavior while assuring TS that it's ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent explanation, saved a bunch of time on my end and will make it clear for anyone looking into the reasons behind this code change. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚀
const [ containerResizeListener, { width: containerWidth } ] = | ||
useResizeObserver(); | ||
if ( Children.count( children ) !== 1 ) { | ||
return null; | ||
} | ||
const imageStyle = { | ||
paddingBottom: | ||
naturalWidth < containerWidth | ||
naturalWidth < ( containerWidth ?? 0 ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent explanation, saved a bunch of time on my end and will make it clear for anyone looking into the reasons behind this code change. Thank you!
/** | ||
* The element to wrap. | ||
*/ | ||
children: React.ReactElement; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The choice of ReactElement
seems correct. We can also broaden the type if necessary.
*/ | ||
import ResponsiveWrapper from '..'; | ||
|
||
const meta: ComponentMeta< typeof ResponsiveWrapper > = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we hide children
's control? It currently shows a react object, which is not really user-friendly to edit
packages/components/CHANGELOG.md
Outdated
@@ -43,6 +43,7 @@ | |||
- `useBaseField`: Convert to TypeScript ([#45712](https://github.com/WordPress/gutenberg/pull/45712)). | |||
- `Dashicon`: Convert to TypeScript ([#45924](https://github.com/WordPress/gutenberg/pull/45924)). | |||
- `PaletteEdit`: add follow up changelog for #45681 and tests [#46095](https://github.com/WordPress/gutenberg/pull/46095). | |||
- `ResponsiveWrapper`: Convert to TypeScript ([#46480](https://github.com/WordPress/gutenberg/pull/46480)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May need repositioning after the last package release
Flaky tests detected in a70bf32. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3858579356
|
Part of #35744
What?
Add TypeScript types and documentation to the
ResponsiveWrapper
component.Why?
So we can ship TS data with the wp-components package.
Testing Instructions
npm run storybook:dev
ResponsiveWrapper
should look good, and the prop descriptions should match the README.md.