Skip to content

Commit

Permalink
Merge branch 'trunk' into ts-sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka authored Jan 7, 2023
2 parents 4cd035b + 47a5fc3 commit 6b3b3e2
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `Dashicon`: remove unnecessary type for `className` prop ([46849](https://github.com/WordPress/gutenberg/pull/46849)).
- `ColorPicker` & `QueryControls`: Replace bottom margin overrides with `__nextHasNoMarginBottom` ([#46448](https://github.com/WordPress/gutenberg/pull/46448)).
- `SandBox`: Convert to TypeScript ([#46478](https://github.com/WordPress/gutenberg/pull/46478)).
- `ResponsiveWrapper`: Convert to TypeScript ([#46480](https://github.com/WordPress/gutenberg/pull/46480)).

### Bug Fix

Expand Down
29 changes: 29 additions & 0 deletions packages/components/src/responsive-wrapper/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ResponsiveWrapper

A wrapper component that maintains its aspect ratio when resized.

## Usage

```jsx
Expand All @@ -14,3 +16,30 @@ const MyResponsiveWrapper = () => (
</ResponsiveWrapper>
);
```

## Props

### `children`: `React.ReactElement`

The element to wrap.

- Required: Yes

### `isInline`: `boolean`

If true, the wrapper will be `span` instead of `div`.

- Required: No
- Default: `false`

### `naturalHeight`: `number`

The intrinsic height of the element to wrap. Will be used to determine the aspect ratio.

- Required: Yes

### `naturalWidth`: `number`

The intrinsic width of the element to wrap. Will be used to determine the aspect ratio.

- Required: Yes
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,41 @@ import classnames from 'classnames';
import { cloneElement, Children } from '@wordpress/element';
import { useResizeObserver } from '@wordpress/compose';

/**
* Internal dependencies
*/
import type { ResponsiveWrapperProps } from './types';

/**
* A wrapper component that maintains its aspect ratio when resized.
*
* ```jsx
* import { ResponsiveWrapper } from '@wordpress/components';
*
* const MyResponsiveWrapper = () => (
* <ResponsiveWrapper naturalWidth={ 2000 } naturalHeight={ 680 }>
* <img
* src="https://s.w.org/style/images/about/WordPress-logotype-standard.png"
* alt="WordPress"
* />
* </ResponsiveWrapper>
* );
* ```
*/
function ResponsiveWrapper( {
naturalWidth,
naturalHeight,
children,
isInline = false,
} ) {
}: ResponsiveWrapperProps ) {
const [ containerResizeListener, { width: containerWidth } ] =
useResizeObserver();
if ( Children.count( children ) !== 1 ) {
return null;
}
const imageStyle = {
paddingBottom:
naturalWidth < containerWidth
naturalWidth < ( containerWidth ?? 0 )
? naturalHeight
: ( naturalHeight / naturalWidth ) * 100 + '%',
};
Expand Down
38 changes: 38 additions & 0 deletions packages/components/src/responsive-wrapper/stories/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* External dependencies
*/
import type { ComponentMeta, ComponentStory } from '@storybook/react';

/**
* Internal dependencies
*/
import ResponsiveWrapper from '..';

const meta: ComponentMeta< typeof ResponsiveWrapper > = {
component: ResponsiveWrapper,
title: 'Components/ResponsiveWrapper',
argTypes: {
children: { control: { type: null } },
},
parameters: {
controls: { expanded: true },
docs: { source: { state: 'open' } },
},
};
export default meta;

const Template: ComponentStory< typeof ResponsiveWrapper > = ( args ) => (
<ResponsiveWrapper { ...args } />
);

export const Default = Template.bind( {} );
Default.args = {
naturalWidth: 2000,
naturalHeight: 680,
children: (
<img
src="https://s.w.org/style/images/about/WordPress-logotype-standard.png"
alt="WordPress"
/>
),
};
20 changes: 20 additions & 0 deletions packages/components/src/responsive-wrapper/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type ResponsiveWrapperProps = {
/**
* The intrinsic width of the element to wrap. Will be used to determine the aspect ratio.
*/
naturalWidth: number;
/**
* The intrinsic height of the element to wrap. Will be used to determine the aspect ratio.
*/
naturalHeight: number;
/**
* The element to wrap.
*/
children: React.ReactElement;
/**
* If true, the wrapper will be `span` instead of `div`.
*
* @default false
*/
isInline?: boolean;
};
1 change: 0 additions & 1 deletion packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"src/palette-edit",
"src/panel",
"src/query-controls",
"src/responsive-wrapper",
"src/toolbar",
"src/toolbar-button",
"src/toolbar-context",
Expand Down

0 comments on commit 6b3b3e2

Please sign in to comment.