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

Flex: Convert component to TypeScript #42537

Merged
merged 7 commits into from
Aug 3, 2022
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

### Internal

- `Flex`, `FlexItem`, `FlexBlock`: Convert to TypeScript ([#42537](https://github.com/WordPress/gutenberg/pull/42537)).
- `InputControl`: Fix incorrect `size` prop passing ([#42793](https://github.com/WordPress/gutenberg/pull/42793)).

## 19.16.0 (2022-07-27)
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/flex/flex-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ See [`flex/README.md#usage`](/packages/components/src/flex/README.md#usage) for

## Props

### display

**Type**: `[CSSProperties['display']]`
### `display`: `CSSProperties['display']`

The CSS display property of `FlexBlock`.

- Required: No
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* Internal dependencies
*/
import { contextConnect } from '../../ui/context';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { View } from '../../view';
import type { FlexBlockProps } from '../types';
import { useFlexBlock } from './hook';

/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexBlockProps, 'div'>} props
* @param {import('react').ForwardedRef<any>} forwardedRef
*/
function FlexBlock( props, forwardedRef ) {
function UnconnectedFlexBlock(
props: WordPressComponentProps< FlexBlockProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const flexBlockProps = useFlexBlock( props );

return <View { ...flexBlockProps } ref={ forwardedRef } />;
}

/**
* `FlexBlock` is a primitive layout component that adaptively resizes content within layout containers like `Flex`.
* `FlexBlock` is a primitive layout component that adaptively resizes content
* within layout containers like `Flex`.
*
* @example
* ```jsx
* import { Flex, FlexBlock } from '@wordpress/components';
*
Expand All @@ -31,6 +36,6 @@ function FlexBlock( props, forwardedRef ) {
* }
* ```
*/
const ConnectedFlexBlock = contextConnect( FlexBlock, 'FlexBlock' );
export const FlexBlock = contextConnect( UnconnectedFlexBlock, 'FlexBlock' );

export default ConnectedFlexBlock;
export default FlexBlock;
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Internal dependencies
*/
import { useContextSystem } from '../../ui/context';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useFlexItem } from '../flex-item';
import type { FlexBlockProps } from '../types';

/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexBlockProps, 'div'>} props
*/
export function useFlexBlock( props ) {
export function useFlexBlock(
props: WordPressComponentProps< FlexBlockProps, 'div' >
) {
const otherProps = useContextSystem( props, 'FlexBlock' );
const flexItemProps = useFlexItem( { isBlock: true, ...otherProps } );

Expand Down
11 changes: 6 additions & 5 deletions packages/components/src/flex/flex-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ See [`flex/README.md#usage`](/packages/components/src/flex/flex/README.md#usage)

## Props

### display

**Type**: `[CSSProperties['display']]`
### `display`: `CSSProperties['display']`

The CSS display property of `FlexItem`.

### isBlock
- Required: No

**Type**: `[boolean]`
### `isBlock`: `boolean`

Determins if `FlexItem` should render as an adaptive full-width block.

- Required: No
- Default: `false`
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* Internal dependencies
*/
import { contextConnect } from '../../ui/context';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { View } from '../../view';
import { useFlexItem } from './hook';
import type { FlexItemProps } from '../types';

/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexItemProps, 'div'>} props
* @param {import('react').ForwardedRef<any>} forwardedRef
*/
function FlexItem( props, forwardedRef ) {
function UnconnectedFlexItem(
props: WordPressComponentProps< FlexItemProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const flexItemProps = useFlexItem( props );

return <View { ...flexItemProps } ref={ forwardedRef } />;
}

/**
* `FlexItem` is a primitive layout component that aligns content within layout containers like `Flex`.
* `FlexItem` is a primitive layout component that aligns content within layout
* containers like `Flex`.
*
* @example
* ```jsx
* import { Flex, FlexItem } from '@wordpress/components';
*
Expand All @@ -31,6 +36,6 @@ function FlexItem( props, forwardedRef ) {
* }
* ```
*/
const ConnectedFlexItem = contextConnect( FlexItem, 'FlexItem' );
export const FlexItem = contextConnect( UnconnectedFlexItem, 'FlexItem' );

export default ConnectedFlexItem;
export default FlexItem;
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/**
* External dependencies
*/
import { css } from '@emotion/react';
import { css, SerializedStyles } from '@emotion/react';

/**
* Internal dependencies
*/
import { useContextSystem } from '../../ui/context';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useFlexContext } from '../context';
import * as styles from '../styles';
import { useCx } from '../../utils/hooks/use-cx';
import type { FlexItemProps } from '../types';

/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexItemProps, 'div'>} props
*/
export function useFlexItem( props ) {
export function useFlexItem(
props: WordPressComponentProps< FlexItemProps, 'div' >
) {
const {
className,
display: displayProp,
isBlock = false,
...otherProps
} = useContextSystem( props, 'FlexItem' );
const sx = {};

const sx: {
Base?: SerializedStyles;
} = {};

const contextDisplay = useFlexContext().flexItemDisplay;

Expand Down
32 changes: 19 additions & 13 deletions packages/components/src/flex/flex/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,44 @@ function Example() {

## Props

##### align

**Type**: `CSSProperties['alignItems']`
### `align`: `CSSProperties['alignItems']`

Aligns children using CSS Flexbox `align-items`. Vertically aligns content if the `direction` is `row`, or horizontally aligns content if the `direction` is `column`.

##### direction
- Required: No
- Default: `center`

**Type**: `[ResponsiveCSSValue<CSSProperties['flexDirection']>]`
### `direction`: `ResponsiveCSSValue<CSSProperties['flexDirection']>`

The direction flow of the children content can be adjusted with `direction`. `column` will align children vertically and `row` will align children horizontally.

##### expanded
- Required: No
- Default: `row`

**Type**: `[boolean]`
### `expanded`: `boolean`

Expands to the maximum available width (if horizontal) or height (if vertical).

##### gap
- Required: No
- Default: `true`

**Type**: `[number | string]`
### `gap`: `number`
Copy link
Contributor

Choose a reason for hiding this comment

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

This is another change that we may consider: I personally find it confusing that we exposed align and justify behaving exactly like the CSS properties, while we use an internal grid system for gap (which is also a valid CSS prop for flex layouts)

@mirka what are your thoughts on this one?

Copy link
Member

Choose a reason for hiding this comment

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

This is a problem with SpaceInput values in general, but I agree that the polymorphic nature of those values can be confusing. (Especially since React style props interpret unitless number values to be pixels by default!) Unfortunately I can't think of an easy way around it, other than removing SpaceInput support from the components themselves and make the consumer use space() from the outside, if they need to.

As an aside, I believe the internal gap-to-margin implementation we have right now was kind of meant as a polyfill, since Safari didn't support flex gap when the Flex component was first written. I don't know if there are active bugs in there, but we can look into removing it in a separate issue. It would be nice to simply rely on native CSS gap.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a problem with SpaceInput values in general, but I agree that the polymorphic nature of those values can be confusing. (Especially since React style props interpret unitless number values to be pixels by default!) Unfortunately I can't think of an easy way around it, other than removing SpaceInput support from the components themselves and make the consumer use space() from the outside, if they need to.

Yeah, this may require some more investigation

As an aside, I believe the internal gap-to-margin implementation we have right now was kind of meant as a polyfill, since Safari didn't support flex gap when the Flex component was first written. I don't know if there are active bugs in there, but we can look into removing it in a separate issue. It would be nice to simply rely on native CSS gap.

I get the same feeling — it would be definitely great to rely on native CSS though

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. I would be great exploring if the native gap can be used. I guess its out of scope for this PR tough.


Spacing in between each child can be adjusted by using `gap`. The value of `gap` works as a multiplier to the library's grid system (base of `4px`).

##### justify
- Required: No
- Default: `2`

**Type**: `[CSSProperties['justifyContent']]`
##### `justify`: `CSSProperties['justifyContent']`

Horizontally aligns content if the `direction` is `row`, or vertically aligns content if the `direction` is `column`.

##### wrap
- Required: No
- Default: `space-between`

**Type**: `[boolean]`
##### `wrap`: `boolean`

Determines if children should wrap.

- Required: No
- Default: `false`
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/**
* External dependencies
*/
import type { ForwardedRef } from 'react';

/**
* Internal dependencies
*/
import { contextConnect } from '../../ui/context';
import { contextConnect, WordPressComponentProps } from '../../ui/context';
import { useFlex } from './hook';
import { FlexContext } from './../context';
import { View } from '../../view';
import type { FlexProps } from '../types';

/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexProps, 'div'>} props
* @param {import('react').ForwardedRef<any>} forwardedRef
*/
function Flex( props, forwardedRef ) {
function UnconnectedFlex(
props: WordPressComponentProps< FlexProps, 'div' >,
forwardedRef: ForwardedRef< any >
) {
const { children, isColumn, ...otherProps } = useFlex( props );

return (
Expand All @@ -29,9 +34,9 @@ function Flex( props, forwardedRef ) {
* horizontally or vertically. `Flex` powers components like `HStack` and
* `VStack`.
*
* `Flex` is used with any of it's two sub-components, `FlexItem` and `FlexBlock`.
* `Flex` is used with any of its two sub-components, `FlexItem` and
* `FlexBlock`.
*
* @example
* ```jsx
* import { Flex, FlexBlock, FlexItem } from '@wordpress/components';
*
Expand All @@ -49,6 +54,6 @@ function Flex( props, forwardedRef ) {
* }
* ```
*/
const ConnectedFlex = contextConnect( Flex, 'Flex' );
export const Flex = contextConnect( UnconnectedFlex, 'Flex' );

export default ConnectedFlex;
export default Flex;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { css } from '@emotion/react';
import { css, SerializedStyles } from '@emotion/react';

/**
* WordPress dependencies
Expand All @@ -12,18 +12,18 @@ import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { useContextSystem } from '../../ui/context';
import { useContextSystem, WordPressComponentProps } from '../../ui/context';
import { useResponsiveValue } from '../../ui/utils/use-responsive-value';
import { space } from '../../ui/utils/space';
import * as styles from '../styles';
import { useCx, rtl } from '../../utils';
import type { FlexProps } from '../types';

function useDeprecatedProps(
props: WordPressComponentProps< FlexProps, 'div' >
): WordPressComponentProps< FlexProps, 'div' > {
const { isReversed, ...otherProps } = props;
ciampo marked this conversation as resolved.
Show resolved Hide resolved

/**
*
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexProps, 'div'>} props
* @return {import('../../ui/context').WordPressComponentProps<import('../types').FlexProps, 'div'>} Props with the deprecated props removed.
*/
function useDeprecatedProps( { isReversed, ...otherProps } ) {
if ( typeof isReversed !== 'undefined' ) {
deprecated( 'Flex isReversed', {
alternative: 'Flex direction="row-reverse" or "column-reverse"',
Expand All @@ -38,10 +38,7 @@ function useDeprecatedProps( { isReversed, ...otherProps } ) {
return otherProps;
}

/**
* @param {import('../../ui/context').WordPressComponentProps<import('../types').FlexProps, 'div'>} props
*/
export function useFlex( props ) {
export function useFlex( props: WordPressComponentProps< FlexProps, 'div' > ) {
const {
align = 'center',
className,
Expand All @@ -67,7 +64,11 @@ export function useFlex( props ) {
const rtlWatchResult = rtl.watch();

const classes = useMemo( () => {
const sx = {};
const sx: {
Base?: SerializedStyles;
Items?: SerializedStyles;
WrapItems?: SerializedStyles;
} = {};

sx.Base = css( {
alignItems: isColumn ? 'normal' : align,
Expand Down
29 changes: 0 additions & 29 deletions packages/components/src/flex/stories/index.js

This file was deleted.

Loading