-
Notifications
You must be signed in to change notification settings - Fork 676
How about remove the Label warpper for Switch? #2124
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
Comments
It will break something... so we can create a new component |
I will try in my project. |
By the way, I find that the Need we set the |
Here is my work, it works well for me: My code!import React from 'react';
import { Box, Label } from 'theme-ui';
import type { CSSProperties as CSSProp } from 'theme-ui';
const GUTTER = 2;
const SIZE = 18;
interface SwitchProps {
gutter?: CSSProp['height'] & CSSProp['width'] & CSSProp['borderRadius'];
size?: CSSProp['height'] & CSSProp['width'] & CSSProp['borderRadius'];
className?: string;
label?: string;
variant?: string;
[key: string]: any;
}
export default React.forwardRef(
(props: SwitchProps, ref: React.ForwardedRef<HTMLInputElement>) => {
let {
gutter = GUTTER,
size = SIZE,
className,
label,
variant = 'switch',
...otherProps
} = props;
if (typeof gutter === 'number') gutter = gutter + 'px';
if (typeof size === 'number') size = size + 'px';
// Hidden checkbox, the really element.
const Checkbox = (
<Box
ref={ref}
as="input"
// @ts-ignore
type="checkbox"
__themeKey="forms"
aria-label={label}
{...otherProps}
sx={{
position: 'absolute',
opacity: 0,
zIndex: -1,
width: 1,
height: 1,
overflow: 'hidden',
}}
/>
);
// The switch just for show.
const Switch = (
<Box
css={{
padding: gutter,
}}
// @ts-ignore
__themeKey="forms"
variant={variant}
className={className}
__css={{
position: 'relative',
flexShrink: 0,
bg: 'gray',
borderRadius: size,
height: `calc(${size} + ${gutter} * 2)`,
width: `calc(${size} * 2 + ${gutter} * 2)`,
'input:disabled ~ &': {
opacity: 0.5,
cursor: 'not-allowed',
},
'& > div': {
display: 'flex',
alignItems: 'center',
borderRadius: '50%',
height: size,
width: size,
bg: 'white',
boxShadow: '0 1px 2px rgba(0, 0, 0, 0.1)',
position: 'relative',
transform: 'translateX(0%)',
transition: `transform 240ms cubic-bezier(0.165, 0.840, 0.440, 1.000)`,
},
'input:checked ~ &': {
bg: 'primary',
'> div': {
transform: 'translateX(100%)',
},
},
}}
>
<Box />
</Box>
);
if (label) {
return (
<Label sx={{ cursor: 'pointer' }}>
{Checkbox}
{Switch}
<span>{label}</span>
</Label>
);
}
return (
<React.Fragment>
{Checkbox}
{Switch}
</React.Fragment>
);
},
); |
The customization here is an interesting idea! The other way to go would be MUI-like |
I do use
startLabel? endLabel? yes, it is a good idea! Do you need a PR? |
Although I think I get it, but I cannot find |
They use |
By the way, do you think that we can change the inner By the way, @lachlanjc are you still 19? |
Personally I'd vote for no more than 2 label props—either the text string & a position one, or the start/endLabel props. It's too confusing when to use endLabel vs label. We'd keep label for a bit as a deprecated compatability shim. cc @hasparus on API preferences I'm 20 now! |
Not sure what you mean by this |
TBH I'd rather have just one {
label?: string,
/** @default "start" */
labelPosition?: "start" | "end",
} |
Was also thinking about this. Works for me! Also then won't be a breaking change which is preferable, & it's more consistent with other components. |
OK. |
I mean, the switch for show have two |
Hello? I'd like to know could I give you PR now... |
Oh phenomenal! Both Piotr & I are unpaid volunteers maintains the projects each with full-time jobs, so we sometimes are less active for a few days when we're doing other things. Especially if we suggested an implementation, you never need permission to open a PR, & we'll just review it as soon as we can! |
PR: #2137 This issue is closed. |
If we want to to let the label be before of the switch, we need:
But I find that Switch is already a Label element. Why? I think this will be better:
The text was updated successfully, but these errors were encountered: