-
Notifications
You must be signed in to change notification settings - Fork 37
feat(connector-i18n): localize the connector dialog #555
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6118633
feat(connector-i18): localize the connector dialog
yixyxiu a6219b6
fix(connector): use own-property check for locale resolution
yixyxiu a106ae1
fix(connector): remove incorrect ARIA semantics from dialog controls
yixyxiu 7860117
fix(app): fix dropdown a11y for collapsed listbox and active option
yixyxiu 310ca4b
chore(connector): drop unused dialog label props and locale keys
yixyxiu f427c11
chore(connector): drop pairing-code locale keys and bind copy-button …
yixyxiu 7de371f
chore(connector): drop aria-label bullet from i18n changeset
yixyxiu 251a7ff
refactor(connector): stop re-exporting internal error scene
yixyxiu 1862e10
refactor(connector): resolve arbitrary BCP 47 tags to built-in locales
yixyxiu 04fff07
chore(connector): add test and test:ci scripts(package.json + lockfile)
yixyxiu 185b075
docs: fix connector language contribution steps(CONTRIBUTING.md)
yixyxiu 07ea092
chore: regenerate pnpm-lock.yaml
yixyxiu 24831f9
refactor(connector): narrow i18n public API and match locales exactly
yixyxiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| "@ckb-ccc/connector": minor | ||
| "@ckb-ccc/connector-react": minor | ||
| --- | ||
|
|
||
| feat(connector): add localization support | ||
|
|
||
| - New `locale` property on `<ccc-connector>` and `ccc.Provider`; English by default, `zh-Hans` built in. The value must be an exact built-in tag; anything else falls back to English. | ||
| - `ccc.connectorLocales` lists the built-in language tags at runtime. | ||
| - Fee rate options are selected by a stable id instead of their display label. | ||
| - Errors raised by the connector itself (camera access, Khie pairing) are translated at render time via `ConnectorError`. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* Shares the visual language of the activity console: monospace uppercase | ||
| labels, hairline borders, gridded dark panel and the mechanical easing. */ | ||
|
|
||
| .root { | ||
| position: relative; | ||
| display: inline-flex; | ||
| } | ||
|
|
||
| .trigger { | ||
| display: flex; | ||
| min-height: 28px; | ||
| align-items: center; | ||
| gap: 6px; | ||
| padding: 0 9px; | ||
| border: 1px solid var(--line); | ||
| color: var(--muted); | ||
| background: transparent; | ||
| font: | ||
| 700 10px/1 "Courier New", | ||
| monospace; | ||
| letter-spacing: 0.08em; | ||
| text-transform: uppercase; | ||
| white-space: nowrap; | ||
| cursor: pointer; | ||
| transition: | ||
| border-color 160ms ease, | ||
| color 160ms ease; | ||
| } | ||
|
|
||
| .trigger:hover, | ||
| .trigger:focus-visible, | ||
| .isOpen .trigger { | ||
| border-color: var(--signal); | ||
| color: var(--signal); | ||
| } | ||
|
|
||
| .trigger:focus-visible { | ||
| outline: none; | ||
| } | ||
|
|
||
| .trigger svg { | ||
| width: 14px; | ||
| height: 14px; | ||
| flex: 0 0 auto; | ||
| stroke-width: 2; | ||
| } | ||
|
|
||
| .chevron { | ||
| transition: transform 300ms var(--easing-mechanical); | ||
| } | ||
|
|
||
| .isOpen .chevron { | ||
| transform: rotate(180deg); | ||
| } | ||
|
|
||
| .menu { | ||
| position: absolute; | ||
| z-index: 30; | ||
| top: calc(100% + 6px); | ||
| right: 0; | ||
| min-width: max(100%, 180px); | ||
| margin: 0; | ||
| padding: 6px 0; | ||
| border: 1px solid var(--line-dark); | ||
| background-color: rgb(17 24 28 / 98%); | ||
| background-image: | ||
| linear-gradient(90deg, rgb(230 238 242 / 2%) 1px, transparent 1px), | ||
| linear-gradient(rgb(230 238 242 / 2%) 1px, transparent 1px); | ||
| background-position: center; | ||
| background-size: 18px 18px; | ||
| list-style: none; | ||
| opacity: 0; | ||
| transform: translateY(-6px); | ||
| pointer-events: none; | ||
| transition: | ||
| opacity 160ms ease, | ||
| transform 300ms var(--easing-mechanical); | ||
| } | ||
|
|
||
| .menu.alignStart { | ||
| right: auto; | ||
| left: 0; | ||
| } | ||
|
|
||
| .isOpen .menu { | ||
| opacity: 1; | ||
| transform: none; | ||
| pointer-events: auto; | ||
| } | ||
|
|
||
| .option { | ||
| display: grid; | ||
| min-height: 32px; | ||
| align-items: center; | ||
| grid-template-columns: 14px minmax(0, 1fr) auto; | ||
| gap: 10px; | ||
| padding: 7px 14px 7px 12px; | ||
| border-left: 2px solid transparent; | ||
| color: var(--ink); | ||
| font: | ||
| 400 11px/1.45 "Courier New", | ||
| monospace; | ||
| white-space: nowrap; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| .option.isActive { | ||
| background: rgb(230 238 242 / 3%); | ||
| } | ||
|
|
||
| .option.isSelected { | ||
| border-left-color: var(--signal); | ||
| } | ||
|
|
||
| .check { | ||
| display: grid; | ||
| width: 14px; | ||
| height: 14px; | ||
| place-items: center; | ||
| color: var(--signal); | ||
| } | ||
|
|
||
| .check svg { | ||
| width: 12px; | ||
| height: 12px; | ||
| stroke-width: 2.5; | ||
| } | ||
|
|
||
| .optionLabel { | ||
| overflow: hidden; | ||
| text-overflow: ellipsis; | ||
| } | ||
|
|
||
| .optionHint { | ||
| color: var(--muted); | ||
| font-size: 9px; | ||
| font-weight: 700; | ||
| letter-spacing: 0.06em; | ||
| text-transform: uppercase; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| "use client"; | ||
|
|
||
| import { Check, ChevronDown } from "lucide-react"; | ||
| import { | ||
| useCallback, | ||
| useEffect, | ||
| useId, | ||
| useRef, | ||
| useState, | ||
| type KeyboardEvent, | ||
| type ReactNode, | ||
| } from "react"; | ||
| import styles from "./dropdown.module.css"; | ||
|
|
||
| export type DropdownOption<T extends string> = { | ||
| value: T; | ||
| label: string; | ||
| /** Small muted text shown at the right of the option, e.g. a code. */ | ||
| hint?: string; | ||
| }; | ||
|
|
||
| export function Dropdown<T extends string>({ | ||
| align = "end", | ||
| className, | ||
| icon, | ||
| label, | ||
| onChange, | ||
| options, | ||
| value, | ||
| }: { | ||
| /** Which edge of the trigger the menu aligns to. */ | ||
| align?: "start" | "end"; | ||
| className?: string; | ||
| /** Optional leading icon rendered inside the trigger. */ | ||
| icon?: ReactNode; | ||
| /** Accessible name of the control; also used as the tooltip. */ | ||
| label: string; | ||
| onChange: (value: T) => void; | ||
| options: readonly DropdownOption<T>[]; | ||
| value: T; | ||
| }) { | ||
| const id = useId(); | ||
| const rootRef = useRef<HTMLDivElement>(null); | ||
| const [open, setOpen] = useState(false); | ||
| const [active, setActive] = useState(() => | ||
| Math.max( | ||
| 0, | ||
| options.findIndex((option) => option.value === value), | ||
| ), | ||
| ); | ||
| const selected = options.find((option) => option.value === value); | ||
|
|
||
| const close = useCallback(() => setOpen(false), []); | ||
| const openMenu = useCallback(() => { | ||
| setActive( | ||
| Math.max( | ||
| 0, | ||
| options.findIndex((option) => option.value === value), | ||
| ), | ||
| ); | ||
| setOpen(true); | ||
| }, [options, value]); | ||
|
|
||
| const select = useCallback( | ||
| (index: number) => { | ||
| const option = options[index]; | ||
| if (option && option.value !== value) { | ||
| onChange(option.value); | ||
| } | ||
| close(); | ||
| }, | ||
| [close, onChange, options, value], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (!open) { | ||
| return; | ||
| } | ||
| const onPointerDown = (event: PointerEvent) => { | ||
| if (!rootRef.current?.contains(event.target as Node)) { | ||
| close(); | ||
| } | ||
| }; | ||
| document.addEventListener("pointerdown", onPointerDown); | ||
| return () => document.removeEventListener("pointerdown", onPointerDown); | ||
| }, [close, open]); | ||
|
|
||
| const onKeyDown = (event: KeyboardEvent<HTMLElement>) => { | ||
| const last = options.length - 1; | ||
| switch (event.key) { | ||
| case "ArrowDown": | ||
| event.preventDefault(); | ||
| if (!open) { | ||
| openMenu(); | ||
| } else { | ||
| setActive((index) => Math.min(last, index + 1)); | ||
| } | ||
| break; | ||
| case "ArrowUp": | ||
| event.preventDefault(); | ||
| if (!open) { | ||
| openMenu(); | ||
| } else { | ||
| setActive((index) => Math.max(0, index - 1)); | ||
| } | ||
| break; | ||
| case "Home": | ||
| if (open) { | ||
| event.preventDefault(); | ||
| setActive(0); | ||
| } | ||
| break; | ||
| case "End": | ||
| if (open) { | ||
| event.preventDefault(); | ||
| setActive(last); | ||
| } | ||
| break; | ||
| case "Enter": | ||
| case " ": | ||
| event.preventDefault(); | ||
| if (open) { | ||
| select(active); | ||
| } else { | ||
| openMenu(); | ||
| } | ||
| break; | ||
| case "Escape": | ||
| if (open) { | ||
| event.preventDefault(); | ||
| close(); | ||
| } | ||
| break; | ||
| case "Tab": | ||
| close(); | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div | ||
| className={`${styles.root} ${open ? styles.isOpen : ""} ${className ?? ""}`} | ||
| ref={rootRef} | ||
| onKeyDown={onKeyDown} | ||
| > | ||
| <button | ||
| type="button" | ||
| className={styles.trigger} | ||
| role="combobox" | ||
| title={label} | ||
| aria-label={label} | ||
| aria-haspopup="listbox" | ||
| aria-expanded={open} | ||
| aria-controls={`${id}-listbox`} | ||
| aria-activedescendant={open ? `${id}-option-${active}` : undefined} | ||
| onClick={() => (open ? close() : openMenu())} | ||
| > | ||
| {icon} | ||
| <span className={styles.value}>{selected?.label ?? value}</span> | ||
| <ChevronDown className={styles.chevron} aria-hidden="true" /> | ||
| </button> | ||
|
|
||
| <ul | ||
| id={`${id}-listbox`} | ||
| role="listbox" | ||
| aria-label={label} | ||
| aria-hidden={!open} | ||
| className={`${styles.menu} ${align === "start" ? styles.alignStart : ""}`} | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| > | ||
| {options.map((option, index) => { | ||
| const isSelected = option.value === value; | ||
| return ( | ||
| <li | ||
| key={option.value} | ||
| id={`${id}-option-${index}`} | ||
| role="option" | ||
| aria-selected={isSelected} | ||
| className={`${styles.option} ${isSelected ? styles.isSelected : ""} ${index === active ? styles.isActive : ""}`} | ||
| onPointerMove={() => setActive(index)} | ||
| onClick={() => select(index)} | ||
| > | ||
| <span className={styles.check} aria-hidden="true"> | ||
| {isSelected ? <Check /> : null} | ||
| </span> | ||
| <span className={styles.optionLabel}>{option.label}</span> | ||
| {option.hint ? ( | ||
| <span className={styles.optionHint}>{option.hint}</span> | ||
| ) : null} | ||
| </li> | ||
| ); | ||
| })} | ||
| </ul> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,13 +34,3 @@ | |
| fill: currentColor; | ||
| stroke: none; | ||
| } | ||
|
|
||
| @media (max-width: 680px) { | ||
| .root { | ||
| gap: 1px; | ||
| } | ||
|
|
||
| .link { | ||
| width: 25px; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.