-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[Colorpicker] Add hex/alpha text fields #8294
Changes from all commits
a60d056
e3edf1a
1493a18
3030d79
ba93568
cc97a88
e219f06
de99b50
2e8ab03
a9273ea
a95526e
dabe77b
069b7b9
cca97f7
fe302dc
38a33b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/polaris': minor | ||
--- | ||
|
||
Add text editor to the `ColorPicker` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,10 @@ | |
"spinnerAccessibilityLabel": "Loading", | ||
"connectedDisclosureAccessibilityLabel": "Related actions" | ||
}, | ||
"ColorPicker": { | ||
"textPickerAccessibilityLabel": "HEX color", | ||
"alphaFieldAccessibilityLabel": "Alpha" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Localization quality issue found The following issues may affect the quality of localized translations if they are not addressed:
Please look out for other instances of this issue in your PR and fix them as well if possible. Questions about these messages? Hop in the #help-localization Slack channel. |
||
}, | ||
"Common": { | ||
"checkbox": "checkbox", | ||
"undo": "Undo", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React, {useCallback, useEffect, useState} from 'react'; | ||
|
||
import {clamp} from '../../../../utilities/clamp'; | ||
import {useI18n} from '../../../../utilities/i18n'; | ||
import {TextField} from '../../../TextField'; | ||
import styles from '../../ColorPicker.scss'; | ||
|
||
export interface AlphaFieldProps { | ||
alpha: number; | ||
onChange(alpha: number): void; | ||
} | ||
|
||
export function AlphaField({alpha, onChange}: AlphaFieldProps) { | ||
const i18n = useI18n(); | ||
|
||
const [percentage, setPercentage] = useState( | ||
clamp(Math.round(alpha * 100) || 0, 0, 100), | ||
); | ||
|
||
const label = i18n.translate( | ||
'Polaris.ColorPicker.alphaFieldAccessibilityLabel', | ||
); | ||
|
||
useEffect(() => { | ||
setPercentage(Math.round(alpha * 100)); | ||
}, [alpha]); | ||
|
||
const handleKeyDown = useCallback( | ||
(event: React.KeyboardEvent<HTMLElement>) => { | ||
const {key, shiftKey} = event; | ||
const step = shiftKey ? 0.05 : 0.01; | ||
if (key === 'ArrowUp') { | ||
event.preventDefault(); | ||
onChange(clamp(alpha + step, 0, 1)); | ||
} else if (key === 'ArrowDown') { | ||
event.preventDefault(); | ||
onChange(clamp(alpha - step, 0, 1)); | ||
} | ||
}, | ||
[alpha, onChange], | ||
); | ||
|
||
const handleTextChange = useCallback( | ||
(value: string) => { | ||
const updatedPercentage = parseInt(value, 10); | ||
|
||
const activeElementRole = document?.activeElement?.getAttribute('role'); | ||
|
||
if (activeElementRole === 'button') { | ||
onChange(updatedPercentage / 100); | ||
} else { | ||
setPercentage(updatedPercentage); | ||
} | ||
}, | ||
[onChange], | ||
); | ||
|
||
const handleBlur = useCallback(() => { | ||
const normalizedPercentage = clamp(percentage, 0, 100); | ||
|
||
if (normalizedPercentage !== null) { | ||
setPercentage(normalizedPercentage); | ||
|
||
const alphaHasChanged = normalizedPercentage !== alpha * 100; | ||
|
||
if (alphaHasChanged) { | ||
onChange(normalizedPercentage / 100); | ||
} | ||
} | ||
}, [alpha, onChange, percentage]); | ||
|
||
return ( | ||
<div | ||
id="AlphaFieldWrapper" | ||
onKeyDown={handleKeyDown} | ||
className={styles.AlphaField} | ||
> | ||
<TextField | ||
suffix="%" | ||
value={percentage.toString()} | ||
label={label} | ||
labelHidden | ||
type="number" | ||
autoComplete="off" | ||
max={100} | ||
min={0} | ||
onChange={handleTextChange} | ||
onBlur={handleBlur} | ||
/> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './AlphaField'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Localization quality issue found
The following issues may affect the quality of localized translations if they are not addressed:
HEX color
for keyPolaris.ColorPicker.textPickerAccessibilityLabel
is very short. Short strings are more likely to be misunderstood by translators without context. Please provide additional context for the translators if possible.Please look out for other instances of this issue in your PR and fix them as well if possible.
Questions about these messages? Hop in the #help-localization Slack channel.