Files
decky-frontend-lib/src/components/Dialog.ts

91 lines
3.6 KiB
TypeScript
Raw Normal View History

import { CSSProperties, FC, ReactNode, RefAttributes } from 'react';
2022-10-24 20:33:40 -04:00
import { CommonUIModule } from '../webpack';
2022-10-02 04:41:47 +03:00
import { FooterLegendProps } from './FooterLegend';
export interface DialogCommonProps extends RefAttributes<HTMLDivElement> {
style?: CSSProperties;
className?: string;
children?: ReactNode;
2022-10-02 04:41:47 +03:00
}
export interface DialogButtonProps extends DialogCommonProps, FooterLegendProps {
/**
* Enables/disables the focus around the button.
2024-05-12 15:48:13 -04:00
*
* @note
* Default value depends on context, so setting it to `false` will enable it.
*/
2022-10-02 04:41:47 +03:00
noFocusRing?: boolean;
/**
* Disables the button - assigned `on*` methods will not be invoked if clicked.
2024-05-12 15:48:13 -04:00
*
* @note
2024-05-12 15:48:13 -04:00
* Depending on where it is, it might still get focus. In such case it can be
* partially disabled separately.
2024-05-12 15:48:13 -04:00
*
* @see focusable.
*/
2022-10-02 04:41:47 +03:00
disabled?: boolean;
/**
* Enables/disables the navigation based focus on button - you won't be able to navigate to
2024-05-12 15:48:13 -04:00
* it via the gamepad or keyboard.
*
* @note
* If set to `false`, it still can be clicked and **WILL** become focused until navigated away.
* Depending on the context of where the button is, even a disabled button can focused.
*/
focusable?: boolean;
2024-05-12 15:48:13 -04:00
2022-10-02 04:41:47 +03:00
onClick?(e: MouseEvent): void;
onPointerDown?(e: PointerEvent): void;
onPointerUp?(e: PointerEvent): void;
onPointerCancel?(e: PointerEvent): void;
onMouseDown?(e: MouseEvent): void;
onMouseUp?(e: MouseEvent): void;
onTouchStart?(e: TouchEvent): void;
onTouchEnd?(e: TouchEvent): void;
onTouchCancel?(e: TouchEvent): void;
onSubmit?(e: SubmitEvent): void;
}
2022-10-24 20:33:40 -04:00
const CommonDialogDivs = Object.values(CommonUIModule).filter(
2024-06-26 22:28:58 -04:00
(m: any) => typeof m === 'object' && m?.render?.toString().includes('createElement("div",{...') ||
m?.render?.toString().includes('createElement("div",Object.assign({},'),
2022-10-24 20:33:40 -04:00
);
const MappedDialogDivs = new Map(
Object.values(CommonDialogDivs).map((m: any) => {
2024-06-26 22:28:58 -04:00
try {
const renderedDiv = m.render({});
// Take only the first class name segment as it identifies the element we want
return [renderedDiv.props.className.split(' ')[0], m];
} catch (e) {
console.error("[DFL:Dialog]: failed to render common dialog component", e);
return [null, null];
}
2022-10-24 20:33:40 -04:00
}),
);
export const DialogHeader = MappedDialogDivs.get('DialogHeader') as FC<DialogCommonProps>;
export const DialogSubHeader = MappedDialogDivs.get('DialogSubHeader') as FC<DialogCommonProps>;
export const DialogFooter = MappedDialogDivs.get('DialogFooter') as FC<DialogCommonProps>;
export const DialogLabel = MappedDialogDivs.get('DialogLabel') as FC<DialogCommonProps>;
export const DialogBodyText = MappedDialogDivs.get('DialogBodyText') as FC<DialogCommonProps>;
export const DialogBody = MappedDialogDivs.get('DialogBody') as FC<DialogCommonProps>;
export const DialogControlsSection = MappedDialogDivs.get('DialogControlsSection') as FC<DialogCommonProps>;
export const DialogControlsSectionHeader = MappedDialogDivs.get('DialogControlsSectionHeader') as FC<DialogCommonProps>;
2022-10-02 04:41:47 +03:00
export const DialogButtonPrimary = Object.values(CommonUIModule).find(
(mod: any) => mod?.render?.toString?.()?.includes('"DialogButton","_DialogLayout","Primary"'),
2022-10-02 04:41:47 +03:00
) as FC<DialogButtonProps>;
export const DialogButtonSecondary = Object.values(CommonUIModule).find(
(mod: any) => mod?.render?.toString?.()?.includes('"DialogButton","_DialogLayout","Secondary"')
2022-10-02 04:41:47 +03:00
) as FC<DialogButtonProps>;
2022-10-24 20:33:40 -04:00
// This is the "main" button. The Primary can act as a submit button,
2022-10-02 04:41:47 +03:00
// therefore secondary is chosen (also for backwards comp. reasons)
export const DialogButton = DialogButtonSecondary;