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

Enhanced role list #755

Merged
merged 9 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions client/src/components/Popover.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { ReactElement, useEffect, useMemo, useRef } from "react";
import React, { ReactElement, ReactNode, useEffect, useMemo, useRef } from "react";
import ReactDOM from "react-dom/client";
import { THEME_CSS_ATTRIBUTES } from "..";

export default function Popover<T extends HTMLElement = HTMLElement>(props: Readonly<{
open: boolean,
children: JSX.Element,
children: ReactNode,
setOpenOrClosed: (open: boolean) => void,
onRender?: (popoverElement: HTMLDivElement, anchorElement?: T | undefined) => void
anchorRef?: React.RefObject<T>,
Expand Down
56 changes: 29 additions & 27 deletions client/src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,7 @@ export default function Select<K extends { toString(): string}>(props: Readonly<
<Popover className="custom-select-options"
open={open}
setOpenOrClosed={handleSetOpen}
onRender={(dropdownElement, buttonElement) => {
if (!buttonElement) return;

const buttonBounds = buttonElement.getBoundingClientRect();
dropdownElement.style.width = `${buttonBounds.width}px`;
dropdownElement.style.left = `${buttonBounds.left}px`;

const spaceAbove = buttonBounds.top;
const spaceBelow = window.innerHeight - buttonBounds.bottom;

const oneRem = parseFloat(getComputedStyle(buttonElement).fontSize);

const maxHeight = (25 - .25) * oneRem;
const optionsHeight = 1 + .5 * oneRem + (dropdownElement.firstElementChild?.clientHeight ?? Infinity);

if (spaceAbove > spaceBelow) {
const newHeight = Math.min(maxHeight, spaceAbove - .25 * oneRem, optionsHeight);
dropdownElement.style.height = `${newHeight}px`;
dropdownElement.style.top = `unset`;
dropdownElement.style.bottom = `${spaceBelow + buttonBounds.height + .25 * oneRem}px`;
} else {
const newHeight = Math.min(maxHeight, spaceBelow - .25 * oneRem, optionsHeight);
dropdownElement.style.height = `${newHeight}px`;
dropdownElement.style.top = `${spaceAbove + buttonBounds.height + .25 * oneRem}px`;
dropdownElement.style.bottom = `unset`;
}
}}
onRender={dropdownPlacementFunction}
anchorRef={ref}
>
<SelectOptions
Expand All @@ -176,6 +150,34 @@ export default function Select<K extends { toString(): string}>(props: Readonly<
</>
}

export function dropdownPlacementFunction(dropdownElement: HTMLElement, buttonElement: HTMLElement | undefined) {
if (!buttonElement) return;

const buttonBounds = buttonElement.getBoundingClientRect();
dropdownElement.style.width = `${buttonBounds.width}px`;
dropdownElement.style.left = `${buttonBounds.left}px`;

const spaceAbove = buttonBounds.top;
const spaceBelow = window.innerHeight - buttonBounds.bottom;

const oneRem = parseFloat(getComputedStyle(buttonElement).fontSize);

const maxHeight = (25 - .25) * oneRem;
const optionsHeight = 1 + .5 * oneRem + (dropdownElement.firstElementChild?.clientHeight ?? Infinity);

if (spaceAbove > spaceBelow) {
const newHeight = Math.min(maxHeight, spaceAbove - .25 * oneRem, optionsHeight);
dropdownElement.style.height = `${newHeight}px`;
dropdownElement.style.top = `unset`;
dropdownElement.style.bottom = `${spaceBelow + buttonBounds.height + .25 * oneRem}px`;
} else {
const newHeight = Math.min(maxHeight, spaceBelow - .25 * oneRem, optionsHeight);
dropdownElement.style.height = `${newHeight}px`;
dropdownElement.style.top = `${spaceAbove + buttonBounds.height + .25 * oneRem}px`;
dropdownElement.style.bottom = `unset`;
}
}

function SelectOptions<K extends { toString(): string}>(props: Readonly<{
searchString?: string,
options: SelectOptionsNoSearch<K>,
Expand Down
28 changes: 13 additions & 15 deletions client/src/components/gameModeSettings/EnabledRoleSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ReactElement, useCallback, useContext, useState } from "react"
import React, { ReactElement, useCallback, useContext, useState } from "react"
import translate from "../../game/lang"
import React from "react"
import StyledText from "../StyledText"
import { RoleOutlineOption, getAllRoles, getRolesFromOutlineOption } from "../../game/roleListState.d";
import { RoleOrRoleSet, getAllRoles, getRolesFromRoleOrRoleSet } from "../../game/roleListState.d";
import { Role } from "../../game/roleState.d";
import { RoleOutlineOptionSelector } from "./OutlineSelector";
import { RoleOrRoleSetSelector } from "./OutlineSelector";
import "./disabledRoleSelector.css"
import Icon from "../Icon";
import { Button } from "../Button";
Expand All @@ -21,14 +20,14 @@ export default function EnabledRoleSelector(props: Readonly<{
}>): ReactElement {
const {enabledRoles} = useContext(GameModeContext);

const [roleOutlineOption, setRoleOutlineOption] = useState<RoleOutlineOption>({ type: "roleSet", roleSet: "town" });
const [roleOrRoleSet, setRoleOrRoleSet] = useState<RoleOrRoleSet>({ type: "roleSet", roleSet: "town" });

const disableOutlineOption = (outline: RoleOutlineOption) => {
props.onDisableRoles(getRolesFromOutlineOption(outline));
const disableOutlineOption = (outline: RoleOrRoleSet) => {
props.onDisableRoles(getRolesFromRoleOrRoleSet(outline));
}

const enableOutlineOption = (outline: RoleOutlineOption) => {
props.onEnableRoles(getRolesFromOutlineOption(outline));
const enableOutlineOption = (outline: RoleOrRoleSet) => {
props.onEnableRoles(getRolesFromRoleOrRoleSet(outline));
}

const disableAll = () => {
Expand All @@ -49,18 +48,17 @@ export default function EnabledRoleSelector(props: Readonly<{

<div className="disabled-role-selector-area">
<Button
onClick={()=>{enableOutlineOption(roleOutlineOption)}}
onClick={()=>{enableOutlineOption(roleOrRoleSet)}}
disabled={props.disabled}
>{translate("menu.enabledRoles.include")}</Button>
<Button
onClick={()=>{disableOutlineOption(roleOutlineOption)}}
onClick={()=>{disableOutlineOption(roleOrRoleSet)}}
disabled={props.disabled}
>{translate("menu.enabledRoles.exclude")}</Button>
<RoleOutlineOptionSelector
excludeAny={true}
<RoleOrRoleSetSelector
disabled={props.disabled}
roleOutlineOption={roleOutlineOption}
onChange={setRoleOutlineOption}
roleOrRoleSet={roleOrRoleSet}
onChange={setRoleOrRoleSet}
/>
</div>
</div>}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/gameModeSettings/GameModesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function GameModesEditor(props: Readonly<{
}, [roleList]);

const addOutline = () => {
setRoleList([...roleList, {type: "any"}]);
setRoleList([...roleList, [{ roleSet: "any" }]]);
}
const removeOutline = (index: number) => {
let newRoleList = [...roleList];
Expand Down
Loading