Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): satisfy Preact 11's per-element ARIA and attribute types

Elian Doran7538edb0c474
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 3e13e6870e..302ff556b6 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -725,7 +725,7 @@ export default function Column({
className={`${isEditing ? "editing" : ""}`}
// A collapsed header opens the column, so it is announced as a button. Open, it
// is a heading, and Space collapses it as a board shortcut like F2.
- role={isCollapsed ? "button" : undefined}
+ role={(isCollapsed ? "button" : undefined) as "heading" | undefined}
aria-expanded={isCollapsed ? false : undefined}
aria-keyshortcuts="Space"
onContextMenu={openMenu}
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index b2d42d39dd..b636d2a5fd 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -1,7 +1,7 @@
import type { Placement } from "@floating-ui/dom";
import type { Tooltip } from "bootstrap";
import clsx from "clsx";
-import { ComponentChildren, HTMLAttributes, RefObject } from "preact";
+import { ButtonHTMLAttributes, ComponentChildren, RefObject } from "preact";
import { CSSProperties, HTMLProps } from "preact/compat";
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
@@ -18,7 +18,7 @@ type DataAttributes = {
export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "className"> {
buttonClassName?: string;
- buttonProps?: Partial<HTMLAttributes<HTMLButtonElement> & DataAttributes>;
+ buttonProps?: Partial<ButtonHTMLAttributes<HTMLButtonElement> & DataAttributes>;
children: ComponentChildren;
title?: string;
dropdownContainerStyle?: CSSProperties;
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsx
index 2024a2484a..300bfea90a 100644
--- a/apps/client/src/widgets/react/FormList.spec.tsx
+++ b/apps/client/src/widgets/react/FormList.spec.tsx
@@ -22,7 +22,7 @@ import FormList, { focusListItem, FormDropdownDivider, FormDropdownSubmenu, Form
describe("FormDropdownDivider", () => {
it("draws a separator among the rows, whose click closes no menu", () => {
const host = document.createElement("div");
- render(<menu><FormDropdownDivider /></menu>, host);
+ render(<menu role="none"><FormDropdownDivider /></menu>, host);
const divider = host.querySelector(".dropdown-divider");
const pageHeard = vi.fn();
host.addEventListener("click", pageHeard);
@@ -39,7 +39,7 @@ describe("FormListHeader", () => {
it("heads the rows after it as text, out of the list's own roles", () => {
const host = document.createElement("div");
// A name the user wrote, such as a board column's status.
- render(<menu><FormListHeader text="<b>Doing</b>" /></menu>, host);
+ render(<menu role="none"><FormListHeader text="<b>Doing</b>" /></menu>, host);
const row = host.querySelector("menu > li");
// Its heading speaks for it, so the row reads as no list item of the menu's.
@@ -52,7 +52,7 @@ describe("FormListItem", () => {
it("lays its row out as one span of icon, gap and content, as a menu's rows are", () => {
const host = document.createElement("div");
render((
- <menu>
+ <menu role="none">
<FormListItem icon="bx bx-copy" iconClassName="use-note-color" badges={[ { text: "new" } ]}>Copy</FormListItem>
</menu>
), host);
@@ -71,7 +71,7 @@ describe("FormListItem", () => {
it("ends its row with a shortcut, then a trailing icon, leaving its own icon standing", () => {
const host = document.createElement("div");
render((
- <menu>
+ <menu role="none">
<FormListItem icon="bx bx-sort" shortcut="Ctrl+S" trailingIcon="bx bx-check">By title</FormListItem>
</menu>
), host);
@@ -86,7 +86,7 @@ describe("FormListItem", () => {
it("shows no shortcuts on a phone, which has no keyboard to press them on", () => {
const host = document.createElement("div");
const rows = () => render((
- <menu>
+ <menu role="none">
<FormListItem shortcut="Ctrl+S">Save</FormListItem>
<FormListItem keyboardShortcut="copyNotesToClipboard">Copy</FormListItem>
</menu>
@@ -112,7 +112,7 @@ describe("FormListItem presses", () => {
const host = document.createElement("div");
const onClick = vi.fn();
render((
- <menu>
+ <menu role="none">
<FormListItem {...props} onClick={onClick}>Size <input className="size" /><input type="checkbox" /></FormListItem>
</menu>
), host);
@@ -160,7 +160,7 @@ describe("FormListItem presses", () => {
describe("FormListCustomItem", () => {
it("holds a control of its own in a row outside any menu too, where there is none to join", () => {
const host = document.createElement("div");
- render(<menu><FormListCustomItem><button className="swatch" /></FormListCustomItem></menu>, host);
+ render(<menu role="none"><FormListCustomItem><button className="swatch" /></FormListCustomItem></menu>, host);
const row = host.querySelector("menu > li");
expect(row?.className).toBe("dropdown-custom-item");
@@ -173,7 +173,7 @@ describe("FormListCustomItem", () => {
describe("FormDropdownSubmenu", () => {
it("renders its title as a header above its rows outside a menu", () => {
const host = document.createElement("div");
- render(<menu><FormDropdownSubmenu icon="bx bx-chip" title="Advanced">
+ render(<menu role="none"><FormDropdownSubmenu icon="bx bx-chip" title="Advanced">
<FormListItem>Reload</FormListItem>
</FormDropdownSubmenu></menu>, host);
const rows = [ ...host.querySelectorAll("menu > li") ];
@@ -226,7 +226,7 @@ describe("FormListToggleableItem", () => {
function mountToggle(props: { onChange(value: boolean): void | Promise<void>, disabled?: boolean }) {
const pageHeard = vi.fn();
const container = mount(
- <menu onClick={pageHeard}>
+ <menu role="none" onClick={pageHeard}>
<FormListToggleableItem title="Shared" currentValue={false} helpPage="R9pX4DGra2Vt" {...props} />
</menu>
);
diff --git a/apps/client/src/widgets/react/FormTextBox.tsx b/apps/client/src/widgets/react/FormTextBox.tsx
index ab77725ebc..aec30921e2 100644
--- a/apps/client/src/widgets/react/FormTextBox.tsx
+++ b/apps/client/src/widgets/react/FormTextBox.tsx
@@ -1,3 +1,4 @@
+import type { AccessibleInputHTMLAttributes } from "preact";
import { useEffect, useRef, type InputHTMLAttributes, type RefObject } from "preact/compat";
export interface FormTextBoxProps extends Omit<InputHTMLAttributes<HTMLInputElement>, "onChange" | "onBlur" | "value"> {
@@ -67,7 +68,6 @@ export default function FormTextBox({ inputRef, className, type, currentValue, o
if (inputRef) inputRef.current = element;
}}
className={`form-control ${className ?? ""}`}
- type={type ?? "text"}
value={currentValue}
onInput={onChange && (e => {
const target = e.currentTarget;
@@ -81,7 +81,10 @@ export default function FormTextBox({ inputRef, className, type, currentValue, o
// fired where anything changed; committing here too would only say it twice.
if (type !== "number") onBlur?.(currentValue);
})}
- {...rest}
+ {...({
+ ...rest,
+ type: type ?? "text"
+ } as AccessibleInputHTMLAttributes<HTMLInputElement>)}
/>
);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/retry_image.tsx b/apps/client/src/widgets/type_widgets/llm_chat/retry_image.tsx
index a35e077445..75baba2a58 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/retry_image.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/retry_image.tsx
@@ -1,3 +1,4 @@
+import type { AccessibleImgHTMLAttributes } from "preact";
import { type ImgHTMLAttributes } from "preact/compat";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
@@ -50,5 +51,11 @@ export function SafeImage({ src, ...rest }: SafeImageProps) {
}, 300 * next);
}, [src]);
- return <img {...rest} src={currentSrc} onError={handleError} />;
+ return (
+ <img
+ {...(rest as AccessibleImgHTMLAttributes<HTMLImageElement>)}
+ src={currentSrc}
+ onError={handleError}
+ />
+ );
}