Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add many custom components and audio module #93

Open
wants to merge 8 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@
"style": "module",
"parser": "typescript"
}
},
"dependencies": {
"react-icons": "^4.11.0"
}
}
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions src/custom-components/CustomButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { ReactNode, FC, useState } from "react";
import { FocusableProps, Focusable, DialogButton } from '../deck-components';
import { GamepadUIAudio, SFXPath, SoundFile, joinClassNames } from '../utils';

export interface CustomButtonProps extends Omit<FocusableProps, 'focusWithinClassName' | 'flow-children' | 'onActivate' | 'onCancel' | 'onClick' | 'children' | 'noFocusRing' | 'onChange'> {
/** The sound effect to use when clicking @default 'deck_ui_default_activation.wav' */
audioSFX?: SoundFile;

/** Whether or not the button sound effect should be disable @default false */
noAudio?: boolean;

/** Whether or not the button should be transparent @default false */
transparent?: boolean;

/** The type of indicator to use when focused @default highlight */
focusMode?: CustomButtonFocusMode;

/** Callback function to be executed when the button is clicked */
onClick?: (e: CustomEvent) => void;

/** CSS class name for the button's container div */
containerClassName?: string;

/** CSS style for the button's container div */
containerStyle?: React.CSSProperties;

/** Whether or not the button should be diabled @default false */
disabled?: boolean;

/** Whether or not the button should be focusable @default false */
focusable?: boolean;

/** Child elements of the component */
children?: ReactNode;
}

/** Type of indicator to use when CustomButton is focused*/
export enum CustomButtonFocusMode {
highlight,
ring
}

/** CSS class names for CustomButton component */
export enum CustomButtonClasses {
buttonContainer = 'custom-button-container',
button = 'custom-button'
}

/** A button component with many customizable options */
export const CustomButton: FC<CustomButtonProps> = ({
audioSFX,
noAudio,
disabled,
focusable,
transparent,
focusMode,
onFocus,
onBlur,
onClick,
style,
className,
containerStyle,
containerClassName,
focusClassName,
onOKActionDescription,
children,
...focusableProps
}) => {
const [focused, setFocused] = useState(false);
const focusStyle = focusMode ?? CustomButtonFocusMode.highlight;

const audioPath: SFXPath = `/sounds/${audioSFX ?? 'deck_ui_default_activation.wav'}`;

const onClicked = (e: CustomEvent) => {
if (!disabled) {
!noAudio && GamepadUIAudio.AudioPlaybackManager.PlayAudioURL(audioPath);
onClick?.(e);
}
};

return (
<Focusable
//@ts-ignore
onClick={onClicked}
className={joinClassNames(CustomButtonClasses.buttonContainer, containerClassName)}
style={containerStyle}
onActivate={focusable ?? true ? onClicked : undefined}
TrainDoctor marked this conversation as resolved.
Show resolved Hide resolved
onFocus={(e) => { setFocused(true); onFocus?.(e); }}
onBlur={(e) => { setFocused(false); onBlur?.(e); }}
noFocusRing={!(focusMode ?? false)}
onOKActionDescription={disabled ? '' : onOKActionDescription}
{...focusableProps}
>
<DialogButton
className={joinClassNames(CustomButtonClasses.button, className, focusStyle === CustomButtonFocusMode.highlight && focused && 'gpfocus', focused && focusClassName)}
style={Object.assign(transparent && (focusStyle === CustomButtonFocusMode.ring || !focused) ? { background: 'transparent' } : {}, style ?? {})}
focusable={false}
disabled={disabled}
>
{children}
</DialogButton>
</Focusable>
);
};
140 changes: 140 additions & 0 deletions src/custom-components/CustomDropdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { SingleDropdownOption, DropdownProps, showContextMenu, Menu, MenuItem, showModal } from '../deck-components';
import { ReactElement, VFC, useState, useEffect } from 'react';
import { FaEllipsis } from 'react-icons/fa6';
import { CustomButtonProps, CustomButton } from './CustomButton';
import { joinClassNames } from '../utils';

export type BaseModalProps = {
onSelectOption: (option: SingleDropdownOption) => void,
rgOptions?: SingleDropdownOption[],
selectedOption?: SingleDropdownOption['data'],
closeModal?: () => void
}

export interface CustomDropdownProps extends Omit<DropdownProps, 'rgOptions' | 'onMenuWillOpen' | 'selectedOption' | 'contextMenuPositionOptions' | 'renderButtonValue'>, Omit<CustomButtonProps, 'audioSFX' | 'noAudio' | 'onClick' | 'children'> {
/** An array of options to choose from */
rgOptions?: SingleDropdownOption[];

/** The selected option data */
selectedOption?: SingleDropdownOption['data'];

/** Whether or not the selection label should be centered @default false */
labelCenter?: boolean;

/** A string to always show in place of the selected option's label */
labelOverride?: string;

/** Whether or not the selection dropdown arrow should be removed @default false */
noDropdownIcon?: boolean;

/** An element to use a replacement for the selection dropdown icon */
customDropdownIcon?: ReactElement;

/** A custom modal to use to select options instead of the default context menu */
customModal?: VFC<BaseModalProps>;

/** CSS style for the selection label div */
labelStyle?: React.CSSProperties;

/** CSS style for the selection label div when it has changed */
labelChangedStyle?: React.CSSProperties;
}

/** CSS class names for CustomDropdown component */
export enum CustomDropdownClasses {
topLevel = 'custom-dropdown-container',
label = 'custom-dropdown-label',
selectionChanged = 'selection-changed'
}

/** A dropdown component with many customizable options */
export const CustomDropdown: VFC<CustomDropdownProps> = ({
rgOptions,
selectedOption: selectedOptionData,
style,
labelStyle,
labelChangedStyle,
containerClassName,
labelOverride,
strDefaultLabel,
labelCenter,
menuLabel,
noDropdownIcon,
customDropdownIcon,
focusMode,
transparent,
onChange,
customModal: CustomModal,
onMenuOpened,
...buttonProps
}) => {
const icon = customDropdownIcon ?? (CustomModal ? <FaEllipsis style={{ margin: 'auto' }} /> : <svg style={{ height: '1em', margin: 'auto' }} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36" fill="none"><path d="M17.98 26.54L3.20996 11.77H32.75L17.98 26.54Z" fill="currentColor"></path></svg>);
const [selected, setSelected] = useState<SingleDropdownOption | undefined>(rgOptions?.find(option => option.data === selectedOptionData));
const [changed, setChanged] = useState(false);

useEffect(() => {
let timeout: number;
if (changed) {
timeout = setTimeout(() => setChanged(false), 15);
}
return () => clearTimeout(timeout);
}, [changed]);

useEffect(() => {
if (selected?.data !== selectedOptionData) {
setChanged(true);
setSelected(rgOptions?.find(option => option.data === selectedOptionData));
}
}, [selectedOptionData, rgOptions?.length]);


const onSelect = (option: SingleDropdownOption) => {
setChanged(true);
setSelected(option);
onChange?.(option);
};

const showDefaultMenu = () => {
showContextMenu(<Menu label={menuLabel ?? ''} >
{rgOptions?.map(option =>
<MenuItem selected={option === selected} onClick={() => onSelect(option)}>
{option.label}
</MenuItem>)}
</Menu>);
onMenuOpened?.();
};

return (
<CustomButton
containerClassName={joinClassNames(CustomDropdownClasses.topLevel, containerClassName)}
style={{ padding: '10px 16px', ...style }}
noAudio={true}
focusMode={focusMode}
transparent={transparent}
onClick={() => {
CustomModal ? showModal(
<CustomModal
onSelectOption={(option) => onSelect(option)}
selectedOption={selected?.data}
rgOptions={rgOptions}
/>
) : rgOptions && showDefaultMenu();
}}
{...buttonProps}
>
<div style={{ display: 'flex', overflow: 'hidden' }}>
<div style={{ overflow: 'hidden', flex: 'auto' }}>
<div style={Object.assign({ textAlign: labelCenter ? 'center' : 'left', minHeight: '20px' }, changed ? labelChangedStyle : labelStyle)} className={joinClassNames(CustomDropdownClasses.label, changed && CustomDropdownClasses.selectionChanged)}>
{labelOverride ?? selected?.label ?? strDefaultLabel}
</div>
</div>
{!noDropdownIcon && (
<div style={{ display: 'flex', marginLeft: '1ch', flex: 'none' }}>
{icon}
</div>
)}
</div>
</CustomButton>
);
};

Loading