diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d8c0843..2bc9e497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - _...Add new stuff here..._ ### 🐞 Bug fixes +- Keep the color picker within the viewport when opening it near an edge, scrolling, or resizing. - Apply layer renames when pressing Enter or leaving the ID field to avoid validating and saving the style on every keystroke. - Add distinct screen reader labels for the anchor, X offset, and Y offset in each variable anchor offset row. - Open `icon-padding` expressions in the expression editor so they can be viewed and edited. diff --git a/e2e/layer-editor.spec.ts b/e2e/layer-editor.spec.ts index 77c6a90e..07e7449a 100644 --- a/e2e/layer-editor.spec.ts +++ b/e2e/layer-editor.spec.ts @@ -178,6 +178,23 @@ describe("layer editor", () => { }); }); + test("keeps the whole color picker visible when opened near the bottom", async () => { + const bgId = await createBackground(); + await when.click("layer-list-item:background:" + bgId); + await when.openColorPickerAtBottom("background-color"); + + await then(get.colorPicker()).shouldBeInViewport(1); + }); + + test("opens the color picker to the left of its field in a right-to-left language", async () => { + const bgId = await createBackground(); + await when.select("maputnik-lang-select", "he"); + await when.click("layer-list-item:background:" + bgId); + await when.openColorPicker("background-color"); + + await then(get.colorPickerSide("background-color")).shouldEqual("left"); + }); + describe("opacity", () => { let bgId: string; beforeEach(async () => { diff --git a/e2e/maputnik-driver.ts b/e2e/maputnik-driver.ts index c21687d3..6f8e7c57 100644 --- a/e2e/maputnik-driver.ts +++ b/e2e/maputnik-driver.ts @@ -373,6 +373,16 @@ export class MaputnikDriver { await input.fill(value); }, + openColorPicker: async (fieldName: string) => { + await this.helper.get.elementByTestId("spec-field:" + fieldName).locator(".maputnik-color").click(); + }, + + openColorPickerAtBottom: async (fieldName: string) => { + const input = this.helper.get.elementByTestId("spec-field:" + fieldName).locator(".maputnik-color"); + await input.evaluate(element => element.scrollIntoView({ block: "end" })); + await input.click(); + }, + /** Sets a plain string spec field (e.g. a pattern), which has no dedicated input test id. */ setStringValue: async (fieldName: string, value: string) => { const input = this.helper.get.elementByTestId("spec-field:" + fieldName).locator("input.maputnik-string"); @@ -404,6 +414,21 @@ export class MaputnikDriver { spriteOptions: () => this.helper.get.element(".maputnik-sprite-option"), + colorPicker: () => this.helper.get.element(".chrome-picker"), + + /** Which side of its input the open color picker sits next to: "left", "right", or "neither" when it isn't beside it. */ + colorPickerSide: (fieldName: string) => this.helper.query(async () => { + const input = await this.helper.get.elementByTestId("spec-field:" + fieldName).locator(".maputnik-color").boundingBox(); + const picker = await this.helper.get.element(".chrome-picker").boundingBox(); + if (!input || !picker) throw new Error("The color picker is not open"); + const maxGap = 16; + const gapOnLeft = input.x - (picker.x + picker.width); + const gapOnRight = picker.x - (input.x + input.width); + if (gapOnLeft >= 0 && gapOnLeft <= maxGap) return "left"; + if (gapOnRight >= 0 && gapOnRight <= maxGap) return "right"; + return "neither"; + }), + spritePixel: (id: string, x: number, y: number) => this.helper.get.canvasPixel(`sprite-preview:${id}`, x, y), canvas: () => this.helper.get.element("canvas"), diff --git a/e2e/playwright-helper.ts b/e2e/playwright-helper.ts index 22c7766d..4b994b15 100644 --- a/e2e/playwright-helper.ts +++ b/e2e/playwright-helper.ts @@ -105,7 +105,8 @@ export class Assertable { } }; shouldNotExist = () => expect(this.locator()).toHaveCount(0); - shouldBeInViewport = () => expect(this.locator().first()).toBeInViewport(); + /** `ratio` is the share of the element that must be visible; 1 means all of it. */ + shouldBeInViewport = (ratio?: number) => expect(this.locator().first()).toBeInViewport({ ratio }); shouldBeFocused = () => expect(this.locator().first()).toBeFocused(); shouldNotBeFocused = () => expect(this.locator().first()).not.toBeFocused(); shouldHaveValue = (value: string) => expect(this.locator().first()).toHaveValue(value); diff --git a/src/components/InputColor.tsx b/src/components/InputColor.tsx index f875d990..4b570e8a 100644 --- a/src/components/InputColor.tsx +++ b/src/components/InputColor.tsx @@ -3,6 +3,8 @@ import Color from "color"; import {ChromePicker, type ColorResult} from "react-color"; import lodash from "lodash"; +const viewportMargin = 8; + function formatColor(color: ColorResult): string { const rgb = color.rgb; return `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${rgb.a})`; @@ -19,11 +21,17 @@ export type InputColorProps = { }; /*** Number fields with support for min, max and units and documentation*/ -export class InputColor extends React.Component { - state = { +type InputColorState = { + pickerOpened: boolean + pickerPosition?: { end: number, top: number } +}; + +export class InputColor extends React.Component { + state: InputColorState = { pickerOpened: false }; colorInput: HTMLInputElement | null = null; + pickerPanel = React.createRef(); constructor (props: InputColorProps) { super(props); @@ -34,27 +42,54 @@ export class InputColor extends React.Component { this.props.onChange(v); } - //TODO: I much rather would do this with absolute positioning - //but I am too stupid to get it to work together with fixed position - //and scrollbars so I have to fallback to JavaScript - calcPickerOffset = () => { - const elem = this.colorInput; - if(elem) { - const pos = elem.getBoundingClientRect(); - return { - top: pos.top, - left: pos.left + 196, - }; + componentDidUpdate(_previousProps: InputColorProps, previousState: InputColorState) { + if (previousState.pickerOpened === this.state.pickerOpened) return; + + if (this.state.pickerOpened) { + this.positionPicker(); + window.addEventListener("resize", this.positionPicker); + window.addEventListener("scroll", this.positionPicker, true); } else { - return { - top: 160, - left: 555, - }; + this.removePositionListeners(); + } + } + + componentWillUnmount() { + this.removePositionListeners(); + } + + removePositionListeners() { + window.removeEventListener("resize", this.positionPicker); + window.removeEventListener("scroll", this.positionPicker, true); + } + + positionPicker = () => { + const input = this.colorInput; + const panel = this.pickerPanel.current; + if (!input || !panel) return; + + const anchor = input.getBoundingClientRect(); + const { width, height } = panel.getBoundingClientRect(); + const viewport = document.documentElement; + const isRtl = getComputedStyle(panel).direction === "rtl"; + // Inline offsets are measured from the viewport's inline-end edge (the right in LTR, the left in RTL), + // so the same placement works in both directions. + const anchorEnd = isRtl ? anchor.left : viewport.clientWidth - anchor.right; + const maxEnd = viewport.clientWidth - width - viewportMargin; + const maxTop = viewport.clientHeight - height - viewportMargin; + const preferredEnd = anchorEnd - width - viewportMargin; + const alternativeEnd = anchorEnd + anchor.width + viewportMargin; + const fitsBetterAtStart = preferredEnd < viewportMargin && alternativeEnd <= maxEnd; + const end = Math.max(viewportMargin, Math.min(maxEnd, fitsBetterAtStart ? alternativeEnd : preferredEnd)); + const top = Math.max(viewportMargin, Math.min(maxTop, anchor.top)); + + if (this.state.pickerPosition?.end !== end || this.state.pickerPosition?.top !== top) { + this.setState({ pickerPosition: { end, top } }); } }; togglePicker = () => { - this.setState({ pickerOpened: !this.state.pickerOpened }); + this.setState(({ pickerOpened }) => ({ pickerOpened: !pickerOpened, pickerPosition: undefined })); }; get color() { @@ -73,7 +108,7 @@ export class InputColor extends React.Component { } render() { - const offset = this.calcPickerOffset(); + const position = this.state.pickerPosition; const currentColor = this.color.object(); const currentChromeColor = { r: currentColor.r, @@ -88,13 +123,26 @@ export class InputColor extends React.Component { style={{ position: "fixed", zIndex: 1, - left: offset.left, - top: offset.top, + insetInlineEnd: position?.end, + top: position?.top, + visibility: position ? "visible" : "hidden", }}> - this.onChangeNoCheck(formatColor(c))} - /> +
+ this.onChangeNoCheck(formatColor(c))} + /> +