From 8552d365eee059c16d911309c2cb952b6eb53c3f Mon Sep 17 00:00:00 2001 From: Peterlits Zo Date: Sat, 19 Feb 2022 07:24:02 +0800 Subject: [PATCH 1/4] feat(components): Add `labelPosition` for `Switch`. Switch has a prop `label` to set its label. And its position is defaultly at the end. To put the label at the start of `Switch` is hard. So I add `labelPosition`, using `gap: 2` instead of the `mr: 2` to make sure that those children's space is right. By the way, I move those inner components outside, to make those code more clear than before. I hope that works. --- packages/components/src/Switch.tsx | 69 +++++++++++-------- packages/docs/src/pages/components/switch.mdx | 19 +++++ 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/packages/components/src/Switch.tsx b/packages/components/src/Switch.tsx index 803548edb..f8ffbdaf3 100644 --- a/packages/components/src/Switch.tsx +++ b/packages/components/src/Switch.tsx @@ -12,6 +12,7 @@ const SIZE = 18 export interface SwitchProps extends Assign, BoxOwnProps> { + labelPosition?: 'start' | 'end', label?: string } @@ -23,7 +24,7 @@ export interface SwitchProps */ export const Switch: ForwardRef = React.forwardRef(function Switch( - { className, label, sx, variant = 'switch', ...rest }, + { className, label, sx, labelPosition = 'end', variant = 'switch', ...rest }, ref ) { const __css: ThemeUICSSObject = { @@ -33,7 +34,6 @@ export const Switch: ForwardRef = borderRadius: SIZE, height: SIZE + GUTTER * 2, width: SIZE * 2 + GUTTER * 2, - mr: 2, 'input:disabled ~ &': { opacity: 0.5, cursor: 'not-allowed', @@ -58,34 +58,45 @@ export const Switch: ForwardRef = }, } + // Inner really checkbox (but hidden) + const reallyHiddenCheckbox = ( + + ); + + // Switch just for show + const switchForShow = ( + + + + ); + return ( - `; From 0e9f6d25884a200fa755e4ecdc92a8ccb66e49f4 Mon Sep 17 00:00:00 2001 From: Peterlits Zo Date: Sat, 19 Feb 2022 00:26:49 +0800 Subject: [PATCH 3/4] test(components): Add test for `Switch` with `label` or `labelPosition`. --- .../test/__snapshots__/index.tsx.snap | 321 ++++++++++++++++++ packages/components/test/index.tsx | 30 ++ 2 files changed, 351 insertions(+) diff --git a/packages/components/test/__snapshots__/index.tsx.snap b/packages/components/test/__snapshots__/index.tsx.snap index 7fca45150..514d31084 100644 --- a/packages/components/test/__snapshots__/index.tsx.snap +++ b/packages/components/test/__snapshots__/index.tsx.snap @@ -1482,6 +1482,327 @@ input:checked~.emotion-2>div { `; +exports[`Switch renders width labelPosition end 1`] = ` +.emotion-0 { + box-sizing: border-box; + margin: 0; + min-width: 0; + width: 100%; + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; + cursor: pointer; + gap: 8px; +} + +.emotion-1 { + box-sizing: border-box; + margin: 0; + min-width: 0; + position: absolute; + opacity: 0; + z-index: -1; + width: 1px; + height: 1px; + overflow: hidden; +} + +.emotion-2 { + box-sizing: border-box; + margin: 0; + min-width: 0; + position: relative; + -webkit-flex-shrink: 0; + -ms-flex-negative: 0; + flex-shrink: 0; + background-color: gray; + border-radius: 18px; + height: 22px; + width: 40px; + padding: 2px; +} + +input:disabled~.emotion-2 { + opacity: 0.5; + cursor: not-allowed; +} + +.emotion-2>div { + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; + -webkit-align-items: center; + -webkit-box-align: center; + -ms-flex-align: center; + align-items: center; + border-radius: 50%; + height: 18px; + width: 18px; + background-color: white; + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); + position: relative; + -webkit-transform: translateX(0%); + -moz-transform: translateX(0%); + -ms-transform: translateX(0%); + transform: translateX(0%); + -webkit-transition: -webkit-transform 240ms cubic-bezier(0.165, 0.840, 0.440, 1.000); + transition: transform 240ms cubic-bezier(0.165, 0.840, 0.440, 1.000); +} + +input:checked~.emotion-2 { + background-color: primary; +} + +input:checked~.emotion-2>div { + -webkit-transform: translateX(100%); + -moz-transform: translateX(100%); + -ms-transform: translateX(100%); + transform: translateX(100%); +} + +.emotion-3 { + box-sizing: border-box; + margin: 0; + min-width: 0; +} + +