mirror of
https://github.com/maputnik/editor.git
synced 2026-12-06 00:34:22 +00:00
Build the layer options menu with downshift instead of react-aria-menubutton (#2409)
Closes #846. `react-aria-menubutton` was only used for the "Layer options" menu in the layer editor header. This PR builds that menu with `downshift`'s `useSelect`, which the project already uses for the autocomplete and sprite inputs, and removes `react-aria-menubutton` and its types from `package.json`. The menu is a new `LayerEditorMenu` component: - Same items, same `data-wd-key`s, and the button keeps the `skip-target-layer-editor` id, so the skip link still focuses it. - The menu closes after an action. The old menu stayed open (`closeOnSelection={false}`); it now uses `useSelect`'s default. - Arrow keys, Enter, Space and Escape work from the button. - In right-to-left languages the menu opens toward the inside of the layer editor. Its position now uses `inset-inline-end`, which replaces the separate `[dir="rtl"]` rule. One more change: the items already had a `disabled` flag for "Move layer up" on the first layer and "Move layer down" on the last one, but `react-aria-menubutton` never used it. `useSelect` supports it (`isItemDisabled`), so those two items are now dimmed and skipped. Screen readers now hear the button as a combobox with a listbox, the pattern `useSelect` follows, instead of a menu button. | Before | After | | --- | --- | |  |  | Both show the menu opened with the keyboard and the second item highlighted. The after image is on the first layer, so "Move layer up" is dimmed. **Performance:** no measurable impact. It's one small menu that only renders its items while open, and the bundle loses one dependency. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues: #846. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [x] Write tests for all new functionality: 5 e2e tests in `layer-editor.spec.ts` ("layer options menu"): keyboard selection, closing after a click, Escape, the disabled item (new `shouldBeDisabled` helper), and the menu staying inside the layer editor in Hebrew. The existing menu tests (delete and move via the menu, skip link) still pass. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. Tested locally: `npm run lint` and `npm run lint-css` clean, `npx vitest run` (59 passed), full e2e with `npx playwright test` (231 passed). I also checked the menu by hand in English and Hebrew. Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the component, the tests and this description. I've reviewed and tested the change and can answer questions about it.
This commit is contained in:
1 parent
cf45de177c
commit
253b584462
9 files changed
+124
-86
No files matched your search
@@ -16,6 +16,7 @@
|
||||
- Change a layer's type from the layer editor; properties the new type doesn't support are removed
|
||||
- Add a "Line metrics" checkbox to GeoJSON sources, which `line-gradient` needs to render
|
||||
- Add a "Use current map view" button to the style settings modal that fills center, zoom, bearing and pitch from the map
|
||||
- Build the layer options menu with `downshift` instead of `react-aria-menubutton`; the menu now closes after an action, and "Move layer up" and "Move layer down" are now disabled for the first and last layer
|
||||
- _...Add new stuff here..._
|
||||
|
||||
### 🐞 Bug fixes
|
||||
|
||||
@@ -648,6 +648,50 @@ describe("layer editor", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("layer options menu", () => {
|
||||
let bgId: string;
|
||||
beforeEach(async () => {
|
||||
bgId = await createBackground();
|
||||
await when.click("layer-list-item:background:" + bgId);
|
||||
});
|
||||
|
||||
test("runs the item chosen with the arrow keys and Enter", async () => {
|
||||
await when.pressOnLayerOptions("ArrowDown", "ArrowDown", "Enter");
|
||||
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
|
||||
layers: [
|
||||
{ id: "background:" + bgId + "-copy", type: "background" },
|
||||
{ id: "background:" + bgId, type: "background" },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test("closes after an item is clicked", async () => {
|
||||
await when.click("skip-target-layer-editor");
|
||||
await when.click("menu-hide-layer");
|
||||
await then(get.elementByTestId("menu-hide-layer")).shouldNotExist();
|
||||
await then(get.styleFromLocalStorage()).shouldDeepNestedInclude({
|
||||
layers: [{ id: "background:" + bgId, layout: { visibility: "none" } }],
|
||||
});
|
||||
});
|
||||
|
||||
test("closes on Escape", async () => {
|
||||
await when.click("skip-target-layer-editor");
|
||||
await when.pressOnLayerOptions("Escape");
|
||||
await then(get.elementByTestId("menu-delete-layer")).shouldNotExist();
|
||||
});
|
||||
|
||||
test("disables Move layer up for the first layer", async () => {
|
||||
await when.click("skip-target-layer-editor");
|
||||
await then(get.elementByTestId("menu-move-layer-up")).shouldBeDisabled();
|
||||
});
|
||||
|
||||
test("opens inside the layer editor in a right-to-left language", async () => {
|
||||
await when.select("maputnik-lang-select", "he");
|
||||
await when.click("skip-target-layer-editor");
|
||||
await then(get.layerOptionsMenuIsInsideLayerEditor()).shouldEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sticky header", () => {
|
||||
test("should keep layer header visible when scrolling properties", async () => {
|
||||
// Setup: Create a layer with many properties (e.g. symbol layer)
|
||||
|
||||
@@ -109,6 +109,14 @@ export class MaputnikDriver {
|
||||
await input.press("Enter");
|
||||
},
|
||||
|
||||
/** Presses each key in turn on the layer options button, which holds focus while its menu is open. */
|
||||
pressOnLayerOptions: async (...keys: string[]) => {
|
||||
const button = this.helper.get.elementByTestId("skip-target-layer-editor");
|
||||
for (const key of keys) {
|
||||
await button.press(key);
|
||||
}
|
||||
},
|
||||
|
||||
deleteLayer: async (id: string, control: "menu" | "trash" = "menu") => {
|
||||
if (control === "trash") {
|
||||
await this.helper.when.hover("layer-list-item:" + id);
|
||||
@@ -433,6 +441,14 @@ export class MaputnikDriver {
|
||||
return "neither";
|
||||
}),
|
||||
|
||||
/** Whether the open layer options menu lies fully within the layer editor's width. */
|
||||
layerOptionsMenuIsInsideLayerEditor: () => this.helper.query(async () => {
|
||||
const editor = await this.helper.get.element(".maputnik-layer-editor").boundingBox();
|
||||
const menu = await this.helper.get.element(".more-menu__menu").boundingBox();
|
||||
if (!editor || !menu) throw new Error("The layer options menu is not open");
|
||||
return menu.x >= editor.x && menu.x + menu.width <= editor.x + editor.width;
|
||||
}),
|
||||
|
||||
/** The expression editor a spec field shows once its value is an expression. */
|
||||
expressionEditor: (fieldName: string) =>
|
||||
this.helper.get.elementByTestId("spec-field-container:" + fieldName).locator(".maputnik-expression-editor"),
|
||||
|
||||
@@ -111,6 +111,7 @@ export class Assertable<T> {
|
||||
shouldNotBeFocused = () => expect(this.locator().first()).not.toBeFocused();
|
||||
shouldHaveValue = (value: string) => expect(this.locator().first()).toHaveValue(value);
|
||||
shouldBeChecked = () => expect(this.locator().first()).toBeChecked();
|
||||
shouldBeDisabled = () => expect(this.locator().first()).toBeDisabled();
|
||||
shouldContainText = async (text: string) => {
|
||||
const locator = this.locator();
|
||||
// Prefer the visible element when a testid resolves to several (only the
|
||||
|
||||
Generated
-40
@@ -49,7 +49,6 @@
|
||||
"prop-types": "^15.8.1",
|
||||
"react": "^19.3.1",
|
||||
"react-accessible-accordion": "^5.0.1",
|
||||
"react-aria-menubutton": "^8.0.0",
|
||||
"react-aria-modal": "^5.0.2",
|
||||
"react-collapse": "^5.1.1",
|
||||
"react-color": "^2.19.3",
|
||||
@@ -82,7 +81,6 @@
|
||||
"@types/lodash.throttle": "^4.1.9",
|
||||
"@types/randomcolor": "^0.5.9",
|
||||
"@types/react": "^19.3.3",
|
||||
"@types/react-aria-menubutton": "^6.2.14",
|
||||
"@types/react-aria-modal": "^5.0.0",
|
||||
"@types/react-collapse": "^5.0.4",
|
||||
"@types/react-color": "^3.0.13",
|
||||
@@ -2679,16 +2677,6 @@
|
||||
"csstype": "^3.2.2"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/react-aria-menubutton": {
|
||||
"version": "6.2.14",
|
||||
"resolved": "https://registry.npmjs.org/@types/react-aria-menubutton/-/react-aria-menubutton-6.2.14.tgz",
|
||||
"integrity": "sha512-RJwtv9FgYLMvBSmUsg+soGjtffFzetA1iMLJIBLAbE2VXtf/QlvltGEwHb1HZ9caddWP/nwyV2HRn3NJQDpqeg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/react": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/react-aria-modal": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/react-aria-modal/-/react-aria-modal-5.0.0.tgz",
|
||||
@@ -5201,12 +5189,6 @@
|
||||
"dev": true,
|
||||
"license": "ISC"
|
||||
},
|
||||
"node_modules/focus-group": {
|
||||
"version": "0.3.1",
|
||||
"resolved": "https://registry.npmjs.org/focus-group/-/focus-group-0.3.1.tgz",
|
||||
"integrity": "sha512-IA01dzk2cStQso/qnt2rWhXCFBZlBfjZmohB9mXUx9feEaJcORAK0FQGvwaApsNNGwzEnqrp/2qTR4lq8PXfnQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/focus-trap": {
|
||||
"version": "7.8.0",
|
||||
"resolved": "https://registry.npmjs.org/focus-trap/-/focus-trap-7.8.0.tgz",
|
||||
@@ -9462,22 +9444,6 @@
|
||||
"react-dom": "^16.3.3 || ^17.0.0 || ^18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/react-aria-menubutton": {
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmjs.org/react-aria-menubutton/-/react-aria-menubutton-8.0.0.tgz",
|
||||
"integrity": "sha512-cMATQDgs14rDJ0yIY701IZpaeGQDLBgDT4fmsjacligHB1YNdFpVQqgw1lRspJcUTiY7e2f8hNGHEwFEm7fuzA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"focus-group": "^0.3.1",
|
||||
"teeny-tap": "^0.2.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react": "^16.3.0 || ^17.0.0 || ^18.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/react-aria-modal": {
|
||||
"version": "5.0.2",
|
||||
"resolved": "https://registry.npmjs.org/react-aria-modal/-/react-aria-modal-5.0.2.tgz",
|
||||
@@ -10689,12 +10655,6 @@
|
||||
"integrity": "sha512-dMCuwDUY6Noz3zVKLefBC4wq+aJV20yc7vWmM7uTNGwtQSFF5kX7OUIUsxuVajz/bdt5Xqdah3/0NLjth+cM5A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/teeny-tap": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/teeny-tap/-/teeny-tap-0.2.0.tgz",
|
||||
"integrity": "sha512-HnA3I2sxRQe/SZgQTQgQvvA17DhfzhBJ1LfSOXZ5VUTbxGLvnAqUef84ZGNNSEbk1ZMEIDeghTHZagJ7LifAgg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/test-exclude": {
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-8.0.0.tgz",
|
||||
|
||||
@@ -66,7 +66,6 @@
|
||||
"prop-types": "^15.8.1",
|
||||
"react": "^19.3.1",
|
||||
"react-accessible-accordion": "^5.0.1",
|
||||
"react-aria-menubutton": "^8.0.0",
|
||||
"react-aria-modal": "^5.0.2",
|
||||
"react-collapse": "^5.1.1",
|
||||
"react-color": "^2.19.3",
|
||||
@@ -116,7 +115,6 @@
|
||||
"@types/lodash.throttle": "^4.1.9",
|
||||
"@types/randomcolor": "^0.5.9",
|
||||
"@types/react": "^19.3.3",
|
||||
"@types/react-aria-menubutton": "^6.2.14",
|
||||
"@types/react-aria-modal": "^5.0.0",
|
||||
"@types/react-collapse": "^5.0.4",
|
||||
"@types/react-color": "^3.0.13",
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import React, { type JSX } from "react";
|
||||
import { Wrapper, Button, Menu, MenuItem } from "react-aria-menubutton";
|
||||
import { Accordion } from "react-accessible-accordion";
|
||||
import { MdMoreVert } from "react-icons/md";
|
||||
import { IconContext } from "react-icons";
|
||||
import { type BackgroundLayerSpecification, type LayerSpecification, type SourceSpecification } from "maplibre-gl";
|
||||
import { v8 } from "@maplibre/maplibre-gl-style-spec";
|
||||
@@ -10,6 +8,7 @@ import { FieldJson } from "./FieldJson";
|
||||
import { FilterEditor } from "./FilterEditor";
|
||||
import { PropertyGroup } from "./PropertyGroup";
|
||||
import { LayerEditorGroup } from "./LayerEditorGroup";
|
||||
import { LayerEditorMenu, type LayerEditorMenuItem } from "./LayerEditorMenu";
|
||||
import { FieldType } from "./FieldType";
|
||||
import { FieldId } from "./FieldId";
|
||||
import { FieldMinZoom } from "./FieldMinZoom";
|
||||
@@ -327,12 +326,7 @@ class LayerEditorInternal extends React.Component<LayerEditorInternalProps, Laye
|
||||
const layout = this.props.layer.layout || {};
|
||||
|
||||
const items: {
|
||||
[key: string]: {
|
||||
text: string,
|
||||
handler: () => void,
|
||||
disabled?: boolean,
|
||||
wdKey?: string
|
||||
}
|
||||
[key: string]: LayerEditorMenuItem
|
||||
} = {
|
||||
delete: {
|
||||
text: t("Delete"),
|
||||
@@ -363,11 +357,6 @@ class LayerEditorInternal extends React.Component<LayerEditorInternalProps, Laye
|
||||
}
|
||||
};
|
||||
|
||||
function handleSelection(id: string, event: React.SyntheticEvent) {
|
||||
event.stopPropagation();
|
||||
items[id].handler();
|
||||
}
|
||||
|
||||
return <IconContext.Provider value={{ size: "14px", color: "#8e8e8e" }}>
|
||||
<section className="maputnik-layer-editor"
|
||||
role="main"
|
||||
@@ -380,31 +369,7 @@ class LayerEditorInternal extends React.Component<LayerEditorInternalProps, Laye
|
||||
{t("Layer")}: {formatLayerId(this.props.layer.id)}
|
||||
</h2>
|
||||
<div className="layer-header__info">
|
||||
<Wrapper
|
||||
className='more-menu'
|
||||
onSelection={(id, event) => handleSelection(id as string, event)}
|
||||
closeOnSelection={false}
|
||||
>
|
||||
<Button
|
||||
id="skip-target-layer-editor"
|
||||
data-wd-key="skip-target-layer-editor"
|
||||
className='more-menu__button'
|
||||
title={"Layer options"}>
|
||||
<MdMoreVert className="more-menu__button__svg" />
|
||||
</Button>
|
||||
<Menu>
|
||||
<ul className="more-menu__menu">
|
||||
{Object.keys(items).map((id) => {
|
||||
const item = items[id];
|
||||
return <li key={id}>
|
||||
<MenuItem value={id} className='more-menu__menu__item' data-wd-key={item.wdKey}>
|
||||
{item.text}
|
||||
</MenuItem>
|
||||
</li>;
|
||||
})}
|
||||
</ul>
|
||||
</Menu>
|
||||
</Wrapper>
|
||||
<LayerEditorMenu items={Object.values(items)} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import classnames from "classnames";
|
||||
import { useSelect } from "downshift";
|
||||
import { MdMoreVert } from "react-icons/md";
|
||||
|
||||
export type LayerEditorMenuItem = {
|
||||
text: string
|
||||
handler: () => void
|
||||
disabled?: boolean
|
||||
wdKey: string
|
||||
};
|
||||
|
||||
type LayerEditorMenuProps = {
|
||||
items: LayerEditorMenuItem[]
|
||||
};
|
||||
|
||||
export function LayerEditorMenu({ items }: LayerEditorMenuProps) {
|
||||
const { isOpen, highlightedIndex, getToggleButtonProps, getMenuProps, getItemProps } = useSelect({
|
||||
items,
|
||||
itemToString: (item) => item?.text ?? "",
|
||||
isItemDisabled: (item) => !!item.disabled,
|
||||
toggleButtonId: "skip-target-layer-editor",
|
||||
// Nothing stays selected, so picking the same action twice runs it twice.
|
||||
selectedItem: null,
|
||||
onSelectedItemChange: ({ selectedItem }) => selectedItem?.handler(),
|
||||
});
|
||||
|
||||
return <div className="more-menu">
|
||||
<span
|
||||
{...getToggleButtonProps({ "aria-label": "Layer options", title: "Layer options" })}
|
||||
data-wd-key="skip-target-layer-editor"
|
||||
className="more-menu__button"
|
||||
>
|
||||
<MdMoreVert className="more-menu__button__svg" />
|
||||
</span>
|
||||
<ul {...getMenuProps()} className="more-menu__menu" hidden={!isOpen}>
|
||||
{isOpen && items.map((item, index) => <li
|
||||
key={item.wdKey}
|
||||
{...getItemProps({ item, index })}
|
||||
className={classnames("more-menu__menu__item", {
|
||||
"more-menu__menu__item--highlighted": highlightedIndex === index,
|
||||
})}
|
||||
data-wd-key={item.wdKey}
|
||||
>
|
||||
{item.text}
|
||||
</li>)}
|
||||
</ul>
|
||||
</div>;
|
||||
}
|
||||
+11
-6
@@ -295,15 +295,10 @@
|
||||
z-index: 9999;
|
||||
background: vars.$color-black;
|
||||
border: solid 1px vars.$color-midgray;
|
||||
right: 0;
|
||||
inset-inline-end: 0;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
[dir="rtl"] &__menu {
|
||||
left: 0;
|
||||
right: auto;
|
||||
}
|
||||
|
||||
&__button__svg {
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
@@ -311,6 +306,16 @@
|
||||
|
||||
&__menu__item {
|
||||
padding: 4px;
|
||||
cursor: pointer;
|
||||
|
||||
&--highlighted {
|
||||
background: vars.$color-midgray;
|
||||
}
|
||||
|
||||
&[aria-disabled="true"] {
|
||||
cursor: default;
|
||||
opacity: 0.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user