@lattice-ui/toggle-group 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
- export { ToggleGroupItem } from "./ToggleGroup/ToggleGroupItem";
2
- export { ToggleGroupRoot, ToggleGroupRoot as ToggleGroup } from "./ToggleGroup/ToggleGroupRoot";
1
+ import { ToggleGroupItem } from "./ToggleGroup/ToggleGroupItem";
2
+ import { ToggleGroupRoot } from "./ToggleGroup/ToggleGroupRoot";
3
+ export declare const ToggleGroup: {
4
+ readonly Root: typeof ToggleGroupRoot;
5
+ readonly Item: typeof ToggleGroupItem;
6
+ };
3
7
  export type { ToggleGroupCommonProps, ToggleGroupContextValue, ToggleGroupItemProps, ToggleGroupMultipleProps, ToggleGroupOrientation, ToggleGroupProps, ToggleGroupSingleProps, ToggleGroupType, ToggleGroupValue, } from "./ToggleGroup/types";
package/out/init.luau CHANGED
@@ -1,8 +1,11 @@
1
1
  -- Compiled with roblox-ts v3.0.0
2
2
  local TS = _G[script]
3
- local exports = {}
4
- exports.ToggleGroupItem = TS.import(script, script, "ToggleGroup", "ToggleGroupItem").ToggleGroupItem
5
- local _ToggleGroupRoot = TS.import(script, script, "ToggleGroup", "ToggleGroupRoot")
6
- exports.ToggleGroupRoot = _ToggleGroupRoot.ToggleGroupRoot
7
- exports.ToggleGroup = _ToggleGroupRoot.ToggleGroupRoot
8
- return exports
3
+ local ToggleGroupItem = TS.import(script, script, "ToggleGroup", "ToggleGroupItem").ToggleGroupItem
4
+ local ToggleGroupRoot = TS.import(script, script, "ToggleGroup", "ToggleGroupRoot").ToggleGroupRoot
5
+ local ToggleGroup = {
6
+ Root = ToggleGroupRoot,
7
+ Item = ToggleGroupItem,
8
+ }
9
+ return {
10
+ ToggleGroup = ToggleGroup,
11
+ }
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@lattice-ui/toggle-group",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "main": "out/init.luau",
6
6
  "types": "out/index.d.ts",
7
+ "files": [
8
+ "out",
9
+ "README.md"
10
+ ],
7
11
  "dependencies": {
8
- "@lattice-ui/core": "0.1.1",
9
- "@lattice-ui/focus": "0.1.1"
12
+ "@lattice-ui/focus": "0.3.0",
13
+ "@lattice-ui/core": "0.3.0"
10
14
  },
11
15
  "devDependencies": {
12
16
  "@rbxts/react": "17.3.7-ts.1",
@@ -18,7 +22,7 @@
18
22
  },
19
23
  "scripts": {
20
24
  "build": "rbxtsc -p tsconfig.json",
21
- "watch": "rbxtsc -p tsconfig.json -w",
22
- "typecheck": "tsc -p tsconfig.typecheck.json"
25
+ "typecheck": "tsc -p tsconfig.typecheck.json",
26
+ "watch": "rbxtsc -p tsconfig.json -w"
23
27
  }
24
28
  }
@@ -1,76 +0,0 @@
1
- import { React, Slot } from "@lattice-ui/core";
2
- import { RovingFocusItem } from "@lattice-ui/focus";
3
- import { useToggleGroupContext } from "./context";
4
- import type { ToggleGroupItemProps } from "./types";
5
-
6
- export function ToggleGroupItem(props: ToggleGroupItemProps) {
7
- const toggleGroupContext = useToggleGroupContext();
8
- const disabled = toggleGroupContext.disabled || props.disabled === true;
9
- const pressed = toggleGroupContext.isPressed(props.value);
10
-
11
- const handleToggle = React.useCallback(() => {
12
- if (disabled) {
13
- return;
14
- }
15
-
16
- toggleGroupContext.toggleValue(props.value);
17
- }, [disabled, props.value, toggleGroupContext]);
18
-
19
- const handleInputBegan = React.useCallback(
20
- (_rbx: TextButton, inputObject: InputObject) => {
21
- if (disabled) {
22
- return;
23
- }
24
-
25
- const keyCode = inputObject.KeyCode;
26
- if (keyCode !== Enum.KeyCode.Return && keyCode !== Enum.KeyCode.Space) {
27
- return;
28
- }
29
-
30
- toggleGroupContext.toggleValue(props.value);
31
- },
32
- [disabled, props.value, toggleGroupContext],
33
- );
34
-
35
- const eventHandlers = React.useMemo(
36
- () => ({
37
- Activated: handleToggle,
38
- InputBegan: handleInputBegan,
39
- }),
40
- [handleInputBegan, handleToggle],
41
- );
42
-
43
- if (props.asChild) {
44
- const child = props.children;
45
- if (!child) {
46
- error("[ToggleGroupItem] `asChild` requires a child element.");
47
- }
48
-
49
- return (
50
- <RovingFocusItem asChild disabled={disabled}>
51
- <Slot Active={!disabled} Event={eventHandlers} Selectable={!disabled}>
52
- {child}
53
- </Slot>
54
- </RovingFocusItem>
55
- );
56
- }
57
-
58
- return (
59
- <RovingFocusItem asChild disabled={disabled}>
60
- <textbutton
61
- Active={!disabled}
62
- AutoButtonColor={false}
63
- BackgroundColor3={pressed ? Color3.fromRGB(88, 142, 255) : Color3.fromRGB(47, 53, 68)}
64
- BorderSizePixel={0}
65
- Event={eventHandlers}
66
- Selectable={!disabled}
67
- Size={UDim2.fromOffset(170, 34)}
68
- Text={props.value}
69
- TextColor3={disabled ? Color3.fromRGB(139, 146, 160) : Color3.fromRGB(236, 241, 249)}
70
- TextSize={15}
71
- >
72
- {props.children}
73
- </textbutton>
74
- </RovingFocusItem>
75
- );
76
- }
@@ -1,121 +0,0 @@
1
- import { React, Slot, useControllableState } from "@lattice-ui/core";
2
- import { RovingFocusGroup } from "@lattice-ui/focus";
3
- import { ToggleGroupContextProvider } from "./context";
4
- import type { ToggleGroupProps } from "./types";
5
-
6
- function normalizeMultiple(value: unknown): Array<string> {
7
- if (!typeIs(value, "table")) {
8
- return [];
9
- }
10
-
11
- const nextValues: Array<string> = [];
12
- const seenValues: Record<string, true> = {};
13
-
14
- for (const entry of value as Array<unknown>) {
15
- if (!typeIs(entry, "string")) {
16
- continue;
17
- }
18
-
19
- if (seenValues[entry]) {
20
- continue;
21
- }
22
-
23
- seenValues[entry] = true;
24
- nextValues.push(entry);
25
- }
26
-
27
- return nextValues;
28
- }
29
-
30
- export function ToggleGroupRoot(props: ToggleGroupProps) {
31
- const disabled = props.disabled === true;
32
- const loop = props.loop ?? true;
33
- const orientation = props.orientation ?? "horizontal";
34
-
35
- const [singleValue, setSingleValueState] = useControllableState<string | undefined>({
36
- value: props.type === "single" ? props.value : undefined,
37
- defaultValue: props.type === "single" ? props.defaultValue : undefined,
38
- onChange: (nextValue) => {
39
- if (props.type === "single") {
40
- props.onValueChange?.(nextValue);
41
- }
42
- },
43
- });
44
-
45
- const [multipleValue, setMultipleValueState] = useControllableState<Array<string>>({
46
- value:
47
- props.type === "multiple" ? (props.value !== undefined ? normalizeMultiple(props.value) : undefined) : undefined,
48
- defaultValue: props.type === "multiple" ? normalizeMultiple(props.defaultValue ?? []) : [],
49
- onChange: (nextValue) => {
50
- if (props.type === "multiple") {
51
- props.onValueChange?.(normalizeMultiple(nextValue));
52
- }
53
- },
54
- });
55
-
56
- const isPressed = React.useCallback(
57
- (itemValue: string) => {
58
- if (props.type === "single") {
59
- return singleValue === itemValue;
60
- }
61
-
62
- return multipleValue.includes(itemValue);
63
- },
64
- [multipleValue, props.type, singleValue],
65
- );
66
-
67
- const toggleValue = React.useCallback(
68
- (itemValue: string) => {
69
- if (disabled) {
70
- return;
71
- }
72
-
73
- if (props.type === "single") {
74
- setSingleValueState(singleValue === itemValue ? undefined : itemValue);
75
- return;
76
- }
77
-
78
- const currentValues = normalizeMultiple(multipleValue);
79
- const nextValues = currentValues.includes(itemValue)
80
- ? currentValues.filter((value) => value !== itemValue)
81
- : [...currentValues, itemValue];
82
- setMultipleValueState(nextValues);
83
- },
84
- [disabled, multipleValue, props.type, setMultipleValueState, setSingleValueState, singleValue],
85
- );
86
-
87
- const contextValue = React.useMemo(
88
- () => ({
89
- type: props.type,
90
- disabled,
91
- orientation,
92
- loop,
93
- isPressed,
94
- toggleValue,
95
- }),
96
- [disabled, isPressed, loop, orientation, props.type, toggleValue],
97
- );
98
-
99
- const groupNode = props.asChild ? (
100
- (() => {
101
- const child = props.children;
102
- if (!React.isValidElement(child)) {
103
- error("[ToggleGroup] `asChild` requires a child element.");
104
- }
105
-
106
- return <Slot>{child}</Slot>;
107
- })()
108
- ) : (
109
- <frame BackgroundTransparency={1} BorderSizePixel={0} Size={UDim2.fromOffset(0, 0)}>
110
- {props.children}
111
- </frame>
112
- );
113
-
114
- return (
115
- <ToggleGroupContextProvider value={contextValue}>
116
- <RovingFocusGroup active={!disabled} autoFocus="none" loop={loop} orientation={orientation}>
117
- {groupNode}
118
- </RovingFocusGroup>
119
- </ToggleGroupContextProvider>
120
- );
121
- }
@@ -1,6 +0,0 @@
1
- import { createStrictContext } from "@lattice-ui/core";
2
- import type { ToggleGroupContextValue } from "./types";
3
-
4
- const [ToggleGroupContextProvider, useToggleGroupContext] = createStrictContext<ToggleGroupContextValue>("ToggleGroup");
5
-
6
- export { ToggleGroupContextProvider, useToggleGroupContext };
@@ -1,45 +0,0 @@
1
- import type React from "@rbxts/react";
2
-
3
- export type ToggleGroupType = "single" | "multiple";
4
- export type ToggleGroupValue = string | string[];
5
- export type ToggleGroupOrientation = "horizontal" | "vertical" | "both";
6
-
7
- export type ToggleGroupCommonProps = {
8
- disabled?: boolean;
9
- loop?: boolean;
10
- orientation?: ToggleGroupOrientation;
11
- asChild?: boolean;
12
- children?: React.ReactNode;
13
- };
14
-
15
- export type ToggleGroupSingleProps = {
16
- type: "single";
17
- value?: string;
18
- defaultValue?: string;
19
- onValueChange?: (value: string | undefined) => void;
20
- };
21
-
22
- export type ToggleGroupMultipleProps = {
23
- type: "multiple";
24
- value?: string[];
25
- defaultValue?: string[];
26
- onValueChange?: (value: string[]) => void;
27
- };
28
-
29
- export type ToggleGroupProps = ToggleGroupCommonProps & (ToggleGroupSingleProps | ToggleGroupMultipleProps);
30
-
31
- export type ToggleGroupContextValue = {
32
- type: ToggleGroupType;
33
- disabled: boolean;
34
- orientation: ToggleGroupOrientation;
35
- loop: boolean;
36
- isPressed: (itemValue: string) => boolean;
37
- toggleValue: (itemValue: string) => void;
38
- };
39
-
40
- export type ToggleGroupItemProps = {
41
- value: string;
42
- disabled?: boolean;
43
- asChild?: boolean;
44
- children?: React.ReactElement;
45
- };
package/src/index.ts DELETED
@@ -1,13 +0,0 @@
1
- export { ToggleGroupItem } from "./ToggleGroup/ToggleGroupItem";
2
- export { ToggleGroupRoot, ToggleGroupRoot as ToggleGroup } from "./ToggleGroup/ToggleGroupRoot";
3
- export type {
4
- ToggleGroupCommonProps,
5
- ToggleGroupContextValue,
6
- ToggleGroupItemProps,
7
- ToggleGroupMultipleProps,
8
- ToggleGroupOrientation,
9
- ToggleGroupProps,
10
- ToggleGroupSingleProps,
11
- ToggleGroupType,
12
- ToggleGroupValue,
13
- } from "./ToggleGroup/types";
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "out",
6
- "declaration": true,
7
- "typeRoots": [
8
- "./node_modules/@rbxts",
9
- "../../node_modules/@rbxts",
10
- "./node_modules/@lattice-ui",
11
- "../../node_modules/@lattice-ui"
12
- ],
13
- "types": ["types", "compiler-types"]
14
- },
15
- "include": ["src"]
16
- }
@@ -1,25 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "noEmit": true,
5
- "baseUrl": "..",
6
- "rootDir": "..",
7
- "paths": {
8
- "@lattice-ui/checkbox": ["checkbox/src/index.ts"],
9
- "@lattice-ui/core": ["core/src/index.ts"],
10
- "@lattice-ui/dialog": ["dialog/src/index.ts"],
11
- "@lattice-ui/focus": ["focus/src/index.ts"],
12
- "@lattice-ui/layer": ["layer/src/index.ts"],
13
- "@lattice-ui/menu": ["menu/src/index.ts"],
14
- "@lattice-ui/popover": ["popover/src/index.ts"],
15
- "@lattice-ui/popper": ["popper/src/index.ts"],
16
- "@lattice-ui/radio-group": ["radio-group/src/index.ts"],
17
- "@lattice-ui/style": ["style/src/index.ts"],
18
- "@lattice-ui/switch": ["switch/src/index.ts"],
19
- "@lattice-ui/system": ["system/src/index.ts"],
20
- "@lattice-ui/tabs": ["tabs/src/index.ts"],
21
- "@lattice-ui/toggle-group": ["toggle-group/src/index.ts"],
22
- "@lattice-ui/tooltip": ["tooltip/src/index.ts"]
23
- }
24
- }
25
- }