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: hold alt to select texts in lists/tables #1113

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions packages/components/src/components/Heading/Heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { FlowComponentProps } from "@/lib/componentFactory/flowComponent";
import { flowComponent } from "@/lib/componentFactory/flowComponent";
import * as Aria from "react-aria-components";
import { TunnelExit, TunnelProvider } from "@mittwald/react-tunnel";
import { useAltKeySelectionProps } from "@/lib/hooks/useAltKeySelectionProps";

export interface HeadingProps extends Aria.HeadingProps, FlowComponentProps {
/** The font size of the heading. */
Expand All @@ -26,6 +27,8 @@ export const Heading = flowComponent("Heading", (props) => {
...rest
} = props;

const altKeySelectionProps = useAltKeySelectionProps(props);

const rootClassName = clsx(
styles.heading,
size && styles[size],
Expand Down Expand Up @@ -59,6 +62,7 @@ export const Heading = flowComponent("Heading", (props) => {
level={level}
className={rootClassName}
{...rest}
{...altKeySelectionProps}
ref={ref}
>
<span className={styles.headingText}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const meta: Meta<typeof List> = {
name="Typ"
defaultSelected={["Domain"]}
/>
<DomainList.Search autoFocus autoSubmit />
<DomainList.Search autoSubmit />
<DomainList.Sorting property="domain" name="A-Z" />
<DomainList.Sorting property="domain" name="Z-A" direction="desc" />
<DomainList.Sorting property="type" name="Typ" defaultEnabled />
Expand Down
7 changes: 6 additions & 1 deletion packages/components/src/components/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { PropsWithClassName } from "@/lib/types/props";
import type { PropsContext } from "@/lib/propsContext";
import { IconContextMenu } from "@/components/Icon/components/icons";
import PropsContextProvider from "@/lib/propsContext/PropsContextProvider";
import { useAltKeySelectionProps } from "@/lib/hooks/useAltKeySelectionProps";

export interface MessageProps extends PropsWithChildren, PropsWithClassName {
/** Determines the color of the message. @default "sender" */
Expand All @@ -24,6 +25,8 @@ export const Message: FC<MessageProps> = (props) => {
className,
);

const altKeySelectionProps = useAltKeySelectionProps(props);

const propsContext: PropsContext = {
Content: { className: styles.content },
Header: {
Expand Down Expand Up @@ -52,7 +55,9 @@ export const Message: FC<MessageProps> = (props) => {

return (
<PropsContextProvider props={propsContext}>
<article className={rootClassName}>{children}</article>
<article className={rootClassName} {...altKeySelectionProps}>
{children}
</article>
</PropsContextProvider>
);
};
Expand Down
13 changes: 9 additions & 4 deletions packages/components/src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { PropsWithChildren } from "react";
import React from "react";
import React, { type PropsWithChildren } from "react";
import * as Aria from "react-aria-components";
import type { PropsContext } from "@/lib/propsContext";
import { ClearPropsContext, PropsContextProvider } from "@/lib/propsContext";
Expand All @@ -11,6 +10,7 @@ import { EmulatedBoldText } from "@/components/EmulatedBoldText";
import { Wrap } from "@/components/Wrap";
import clsx from "clsx";
import styles from "./Text.module.scss";
import { useAltKeySelectionProps } from "@/lib/hooks/useAltKeySelectionProps";

export interface TextProps
extends PropsWithChildren,
Expand All @@ -34,6 +34,11 @@ export const Text = flowComponent("Text", (props) => {
...rest
} = props;

const commonProps = {
ref,
...useAltKeySelectionProps(rest),
};

const rootClassName = clsx(styles.text, color && styles[color], className);

const textProps = { ...rest, className: rootClassName };
Expand All @@ -55,7 +60,7 @@ export const Text = flowComponent("Text", (props) => {
if (!props.slot) {
const Element = elementType;
return (
<Element {...textProps} ref={ref}>
<Element {...commonProps} {...textProps}>
{childrenElement}
</Element>
);
Expand All @@ -68,7 +73,7 @@ export const Text = flowComponent("Text", (props) => {

return (
<ClearPropsContext>
<Aria.Text {...textProps} elementType={elementType} ref={ref}>
<Aria.Text {...commonProps} {...textProps} elementType={elementType}>
{childrenElement}
</Aria.Text>
</ClearPropsContext>
Expand Down
68 changes: 68 additions & 0 deletions packages/components/src/lib/hooks/useAltKeySelectionProps.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
type CSSProperties,
type MouseEvent,
type MouseEventHandler,
type PointerEvent,
type PointerEventHandler,
useEffect,
useState,
} from "react";

export const useAltKeySelectionProps = (props: NonNullable<unknown>) => {
const styleFromProps =
typeof props === "object" &&
"style" in props &&
typeof props.style === "object"
? (props.style as CSSProperties)
: {};

const [altKeyHolding, setAltKeyHolding] = useState(false);

const onKeyUp = (e: KeyboardEvent) => {
if (e.key === "Alt") {
setAltKeyHolding(false);
}
};
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Alt") {
setAltKeyHolding(true);
}
};

useEffect(() => {
window.addEventListener("keyup", onKeyUp);
window.addEventListener("keydown", onKeyDown);
return () => {
window.removeEventListener("keyup", onKeyUp);
window.removeEventListener("keydown", onKeyDown);
};
}, []);

const handlePointerDown: PointerEventHandler = (e) => {
handlePointerOrMouseDown(e);
};
const handleMouseDown: MouseEventHandler = (e) => {
handlePointerOrMouseDown(e);
};
const handlePointerOrMouseDown = (e: MouseEvent | PointerEvent) => {
if (altKeyHolding) {
e.stopPropagation();
}
};
const selectionStyle = altKeyHolding
? {
cursor: "text",
}
: {};

const style: CSSProperties = {
...selectionStyle,
...styleFromProps,
};

return {
style,
onPointerDown: handlePointerDown,
onMouseDown: handleMouseDown,
};
};
Loading