jsmorabito_obsidian-commander/src/ui/components/mobileModifyComponent.tsx
carlesba ecc9c06fd9
Fix mobile toolbar icons not responding to clicks
Replace this.forceUpdate() with useState hook in functional components.
In Preact functional components, 'this' is undefined, so this.forceUpdate()
silently threw errors, preventing all callback handlers (remove, reorder,
rename, icon change, mode change) from completing on mobile.
2026-06-25 15:37:11 +02:00

88 lines
2.2 KiB
TypeScript

import { h } from "preact";
import { useEffect, useState } from "preact/hooks";
import t from "src/l10n";
import { ObsidianIcon } from "src/util";
import MobileModifyModal from "../mobileModifyModal";
import { ColorPicker } from "./ColorPicker";
import CommanderPlugin from "src/main";
export default function MobileModifyComponent({
plugin,
modal: controller,
}: {
plugin: CommanderPlugin;
modal: MobileModifyModal;
}): h.JSX.Element {
const [, rerender] = useState(0);
useEffect(() => {
const update = (): void => {
rerender((n) => n + 1);
};
addEventListener("cmdr-icon-changed", update);
return () => removeEventListener("cmdr-icon-changed", update);
}, []);
return (
<div className="cmdr-mobile-modify-grid">
<div
className="cmdr-mobile-modify-option"
onClick={controller.handleNewIcon}
>
<span>{t("Icon")}</span>
<span className="cmdr-flex cmdr-gap-1">
<ObsidianIcon
icon={controller.pair.icon}
size={20}
className="clickable-icon"
style={{ marginRight: "0px" }}
/>
<ColorPicker
initialColor={controller.pair.color ?? "#000"}
onChange={controller.handleColorChange}
/>
</span>
</div>
<div className="cmdr-mobile-modify-option">
<span>{t("Name")}</span>
<input
onBlur={({ currentTarget }): void =>
controller.handleRename(currentTarget.value)
}
type="text"
placeholder={t("Custom Name")}
value={controller.pair.name}
/>
</div>
<div className="cmdr-mobile-modify-option">
<select
className="dropdown"
value={controller.pair.mode}
onChange={({ currentTarget }): void =>
controller.handleModeChange(currentTarget.value)
}
>
<option value="any">
{t("Add command to all devices")}
</option>
<option value="mobile">
{t("Add command only to mobile devices")}
</option>
<option value="desktop">
{t("Add command only to desktop devices")}
</option>
<option value={plugin.app.appId}>
{t("Add command only to this device")}
</option>
</select>
</div>
<div className="modal-button-container">
<button
className="mod-cta"
onClick={(): void => controller.close()}
>
{t("Done")}
</button>
</div>
</div>
);
}