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

Hack Days 34 Unified Color Picker #9843

Closed
wants to merge 8 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .changeset/mighty-turkeys-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/polaris': minor
'polaris.shopify.com': minor
---

Add text editor to the `ColorPicker`
4 changes: 4 additions & 0 deletions polaris-react/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"spinnerAccessibilityLabel": "Loading",
"connectedDisclosureAccessibilityLabel": "Related actions"
},
"ColorPicker": {
"textPickerAccessibilityLabel": "HEX color",
"alphaFieldAccessibilityLabel": "Alpha"
},
"Common": {
"checkbox": "checkbox",
"undo": "Undo",
Expand Down
46 changes: 46 additions & 0 deletions polaris-react/src/components/ColorPicker/ColorPicker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
width: var(--pc-color-picker-size);

.AlphaAllowed.TextAllowed & {
width: 224px;
}

.fullWidth & {
width: auto;
flex-grow: 1;
Expand Down Expand Up @@ -187,3 +191,45 @@ $vertical-picker-border-radius: calc(var(--pc-color-picker-size) * 0.5);
width: 100%;
cursor: pointer;
}

.TextFields {
display: flex;
}

.TextPicker {
width: 148px;
margin-top: var(--p-space-2);

&.AlphaAllowed {
width: 136px;
}

.fullWidth & {
width: auto;
flex-grow: 1;
}
}

.TextFieldSwatch {
width: control-height();
height: control-height();
margin: var(--p-space-2) var(--p-space-2) 0 0;
border: var(--p-border-width-1) solid var(--p-color-border-strong);
border-radius: 3px;

&.AlphaAllowed {
@include checkers;
}
}

.SwatchBackground {
width: 100%;
height: 100%;
border-radius: inherit;
box-shadow: inherit;
}

.AlphaField {
flex: 0 0 100px;
margin: var(--p-space-2) 0 0 var(--p-space-2);
}
79 changes: 30 additions & 49 deletions polaris-react/src/components/ColorPicker/ColorPicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,63 +44,44 @@ export function WithTransparentValueFullWidth() {
return <ColorPicker fullWidth onChange={setColor} color={color} allowAlpha />;
}

export function WithHexTextField() {
export function WithTextFields() {
const [color, setColor] = useState({
hue: 300,
saturation: 0.7,
hue: 120,
brightness: 1,
saturation: 1,
});

const [hex, setHex] = useState(hsbToHex(color));

const handleBlur = useCallback(() => {
setColor(rgbToHsb(hexToRgb(hex)));
}, [hex]);

useEffect(() => {
setHex(hsbToHex(color));
}, [color]);
return <ColorPicker onChange={setColor} color={color} textEditor />;
}

const styles = {
Wrapper: {
display: 'grid',
gap: 'var(--p-space-2)',
gridTemplateColumns: 'auto 1fr',
maxWidth: 'fit-content',
},
Picker: {
gridColumn: '1 / 3',
},
Tile: {
minHeight: 'calc(100% - 0.125rem)',
aspectRatio: '1 / 1',
borderRadius: 'var(--p-border-radius-1)',
border: 'var(--p-border-width-1) solid var(--p-color-border-subdued)',
backgroundColor: hsbToHex(color),
},
TextField: {
maxWidth: 'fit-content',
},
};
export function WithTransparentValueAndTextFields() {
const [color, setColor] = useState({
hue: 300,
brightness: 1,
saturation: 0.7,
alpha: 0.7,
});

return (
<div style={styles.Wrapper}>
<div style={styles.Picker}>
<ColorPicker onChange={setColor} color={color} />
</div>
<ColorPicker onChange={setColor} color={color} allowAlpha textEditor />
);
}

<div style={styles.Tile} />
export function WithTransparentValueAndTextFieldsFullWidth() {
const [color, setColor] = useState({
hue: 300,
brightness: 1,
saturation: 0.7,
alpha: 0.7,
});

<div style={styles.TextField}>
<TextField
label=""
prefix="#"
value={hex.replace('#', '')}
onChange={setHex}
onBlur={handleBlur}
autoComplete="off"
/>
</div>
</div>
return (
<ColorPicker
onChange={setColor}
color={color}
allowAlpha
fullWidth
textEditor
/>
);
}
Loading