From 2c3577b1a0495c5463c8d2c66787cc596d2d3559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20Walb=C3=B8=20Johnsg=C3=A5rd?= Date: Wed, 28 Jul 2021 14:00:36 +0200 Subject: [PATCH 01/10] Components: Fix RTL on `Flex` component --- packages/components/src/flex/flex/hook.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/components/src/flex/flex/hook.js b/packages/components/src/flex/flex/hook.js index e85634f6c3c15d..802cecc5b13f62 100644 --- a/packages/components/src/flex/flex/hook.js +++ b/packages/components/src/flex/flex/hook.js @@ -6,6 +6,7 @@ import { css } from '@emotion/react'; /** * WordPress dependencies */ +import { isRTL } from '@wordpress/i18n'; import { useMemo } from '@wordpress/element'; import deprecated from '@wordpress/deprecated'; @@ -67,6 +68,8 @@ export function useFlex( props ) { const classes = useMemo( () => { const sx = {}; + const marginLeftProperty = isRTL() ? 'marginRight' : 'marginLeft'; + const marginRightProperty = isRTL() ? 'marginLeft' : 'marginRight'; sx.Base = css( { alignItems: isColumn ? 'normal' : align, @@ -88,8 +91,9 @@ export function useFlex( props ) { */ '> * + *:not(marquee)': { marginTop: isColumn ? space( gap ) : undefined, - marginRight: ! isColumn && isReverse ? space( gap ) : undefined, - marginLeft: + [ marginRightProperty ]: + ! isColumn && isReverse ? space( gap ) : undefined, + [ marginLeftProperty ]: ! isColumn && ! isReverse ? space( gap ) : undefined, }, } ); @@ -97,13 +101,15 @@ export function useFlex( props ) { sx.WrapItems = css( { '> *:not(marquee)': { marginBottom: space( gap ), - marginLeft: ! isColumn && isReverse ? space( gap ) : undefined, - marginRight: + [ marginLeftProperty ]: + ! isColumn && isReverse ? space( gap ) : undefined, + [ marginRightProperty ]: ! isColumn && ! isReverse ? space( gap ) : undefined, }, '> *:last-child:not(marquee)': { - marginLeft: ! isColumn && isReverse ? 0 : undefined, - marginRight: ! isColumn && ! isReverse ? 0 : undefined, + [ marginLeftProperty ]: ! isColumn && isReverse ? 0 : undefined, + [ marginRightProperty ]: + ! isColumn && ! isReverse ? 0 : undefined, }, } ); From c8c1017d254c48814a81001cf2fe90a765d66269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20Walb=C3=B8=20Johnsg=C3=A5rd?= Date: Wed, 28 Jul 2021 17:17:24 +0200 Subject: [PATCH 02/10] Use RTL util --- packages/components/src/flex/flex/hook.js | 71 ++++++++++++----------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/packages/components/src/flex/flex/hook.js b/packages/components/src/flex/flex/hook.js index 802cecc5b13f62..e1f7a57f8b2fa9 100644 --- a/packages/components/src/flex/flex/hook.js +++ b/packages/components/src/flex/flex/hook.js @@ -6,7 +6,6 @@ import { css } from '@emotion/react'; /** * WordPress dependencies */ -import { isRTL } from '@wordpress/i18n'; import { useMemo } from '@wordpress/element'; import deprecated from '@wordpress/deprecated'; @@ -17,7 +16,7 @@ import { useContextSystem } from '../../ui/context'; import { useResponsiveValue } from '../../ui/utils/use-responsive-value'; import { space } from '../../ui/utils/space'; import * as styles from '../styles'; -import { useCx } from '../../utils/hooks/use-cx'; +import { useCx, rtl } from '../../utils'; /** * @@ -68,8 +67,6 @@ export function useFlex( props ) { const classes = useMemo( () => { const sx = {}; - const marginLeftProperty = isRTL() ? 'marginRight' : 'marginLeft'; - const marginRightProperty = isRTL() ? 'marginLeft' : 'marginRight'; sx.Base = css( { alignItems: isColumn ? 'normal' : align, @@ -81,37 +78,43 @@ export function useFlex( props ) { marginBottom: wrap ? `calc(${ space( gap ) } * -1)` : undefined, } ); - sx.Items = css( { - /** - * Workaround to optimize DOM rendering. - * We'll enhance alignment with naive parent flex assumptions. - * - * Trade-off: - * Far less DOM less. However, UI rendering is not as reliable. - */ - '> * + *:not(marquee)': { - marginTop: isColumn ? space( gap ) : undefined, - [ marginRightProperty ]: - ! isColumn && isReverse ? space( gap ) : undefined, - [ marginLeftProperty ]: - ! isColumn && ! isReverse ? space( gap ) : undefined, - }, - } ); + /** + * Workaround to optimize DOM rendering. + * We'll enhance alignment with naive parent flex assumptions. + * + * Trade-off: + * Far less DOM less. However, UI rendering is not as reliable. + */ + sx.Items = css` + > * + *:not( marquee ) { + margin-top: ${ isColumn ? space( gap ) : undefined }; + ${ rtl( { + marginLeft: + ! isColumn && ! isReverse ? space( gap ) : undefined, + marginRight: + ! isColumn && isReverse ? space( gap ) : undefined, + } )() } + } + `; - sx.WrapItems = css( { - '> *:not(marquee)': { - marginBottom: space( gap ), - [ marginLeftProperty ]: - ! isColumn && isReverse ? space( gap ) : undefined, - [ marginRightProperty ]: - ! isColumn && ! isReverse ? space( gap ) : undefined, - }, - '> *:last-child:not(marquee)': { - [ marginLeftProperty ]: ! isColumn && isReverse ? 0 : undefined, - [ marginRightProperty ]: - ! isColumn && ! isReverse ? 0 : undefined, - }, - } ); + sx.WrapItems = css` + > *:not( marquee ) { + margin-bottom: ${ space( gap ) }; + ${ rtl( { + marginLeft: + ! isColumn && isReverse ? space( gap ) : undefined, + marginRight: + ! isColumn && ! isReverse ? space( gap ) : undefined, + } )() } + } + + > *:last-child:not( marquee ) { + ${ rtl( { + marginLeft: ! isColumn && isReverse ? 0 : undefined, + marginRight: ! isColumn && ! isReverse ? 0 : undefined, + } )() } + } + `; return cx( styles.Flex, From 2f10b2e4f5149551f7d4e11a72892e9ab2aedfdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petter=20Walb=C3=B8=20Johnsg=C3=A5rd?= Date: Wed, 28 Jul 2021 17:30:53 +0200 Subject: [PATCH 03/10] Update snapshots --- .../test/__snapshots__/index.js.snap | 2 +- .../src/card/test/__snapshots__/index.js.snap | 40 +++++++++---------- .../src/flex/test/__snapshots__/index.js.snap | 2 +- .../h-stack/test/__snapshots__/index.js.snap | 6 +-- .../test/__snapshots__/index.js.snap | 2 +- .../v-stack/test/__snapshots__/index.js.snap | 6 +-- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/components/src/base-field/test/__snapshots__/index.js.snap b/packages/components/src/base-field/test/__snapshots__/index.js.snap index 99ee459d1a2e2e..189db5c48bf126 100644 --- a/packages/components/src/base-field/test/__snapshots__/index.js.snap +++ b/packages/components/src/base-field/test/__snapshots__/index.js.snap @@ -105,7 +105,7 @@ exports[`base field should render correctly 1`] = ` width: 100%; } -.emotion-0>*+*:not(marquee) { +.emotion-0>*+*:not( marquee ) { margin-left: calc(4px * 2); } diff --git a/packages/components/src/card/test/__snapshots__/index.js.snap b/packages/components/src/card/test/__snapshots__/index.js.snap index eb44001d513d37..88a4c8801e75a8 100644 --- a/packages/components/src/card/test/__snapshots__/index.js.snap +++ b/packages/components/src/card/test/__snapshots__/index.js.snap @@ -42,8 +42,8 @@ Snapshot Diff: @@ -1,8 +1,8 @@