@lattice-ui/checkbox 0.1.1
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/Checkbox/CheckboxIndicator.d.ts +3 -0
- package/out/Checkbox/CheckboxIndicator.luau +32 -0
- package/out/Checkbox/CheckboxRoot.d.ts +3 -0
- package/out/Checkbox/CheckboxRoot.luau +81 -0
- package/out/Checkbox/context.d.ts +3 -0
- package/out/Checkbox/context.luau +10 -0
- package/out/Checkbox/types.d.ts +23 -0
- package/out/Checkbox/types.luau +2 -0
- package/out/index.d.ts +3 -0
- package/out/init.luau +8 -0
- package/package.json +23 -0
- package/src/Checkbox/CheckboxIndicator.tsx +34 -0
- package/src/Checkbox/CheckboxRoot.tsx +86 -0
- package/src/Checkbox/context.ts +6 -0
- package/src/Checkbox/types.ts +28 -0
- package/src/index.ts +3 -0
- package/tsconfig.json +16 -0
- package/tsconfig.typecheck.json +25 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local TS = _G[script]
|
|
3
|
+
local _core = TS.import(script, TS.getModule(script, "@lattice-ui", "core").out)
|
|
4
|
+
local React = _core.React
|
|
5
|
+
local Slot = _core.Slot
|
|
6
|
+
local useCheckboxContext = TS.import(script, script.Parent, "context").useCheckboxContext
|
|
7
|
+
local function CheckboxIndicator(props)
|
|
8
|
+
local checkboxContext = useCheckboxContext()
|
|
9
|
+
local visible = checkboxContext.checked ~= false
|
|
10
|
+
local forceMount = props.forceMount == true
|
|
11
|
+
if not visible and not forceMount then
|
|
12
|
+
return nil
|
|
13
|
+
end
|
|
14
|
+
local child = props.children
|
|
15
|
+
if props.asChild then
|
|
16
|
+
if not React.isValidElement(child) then
|
|
17
|
+
error("[CheckboxIndicator] `asChild` requires a child element.")
|
|
18
|
+
end
|
|
19
|
+
return React.createElement(Slot, {
|
|
20
|
+
Visible = visible,
|
|
21
|
+
}, child)
|
|
22
|
+
end
|
|
23
|
+
return React.createElement("frame", {
|
|
24
|
+
BackgroundColor3 = Color3.fromRGB(240, 244, 252),
|
|
25
|
+
BorderSizePixel = 0,
|
|
26
|
+
Size = UDim2.fromOffset(12, 12),
|
|
27
|
+
Visible = visible,
|
|
28
|
+
}, child)
|
|
29
|
+
end
|
|
30
|
+
return {
|
|
31
|
+
CheckboxIndicator = CheckboxIndicator,
|
|
32
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local TS = _G[script]
|
|
3
|
+
local _core = TS.import(script, TS.getModule(script, "@lattice-ui", "core").out)
|
|
4
|
+
local React = _core.React
|
|
5
|
+
local Slot = _core.Slot
|
|
6
|
+
local useControllableState = _core.useControllableState
|
|
7
|
+
local CheckboxContextProvider = TS.import(script, script.Parent, "context").CheckboxContextProvider
|
|
8
|
+
local function getNextCheckedState(checked)
|
|
9
|
+
if checked == "indeterminate" then
|
|
10
|
+
return true
|
|
11
|
+
end
|
|
12
|
+
return not checked
|
|
13
|
+
end
|
|
14
|
+
local function CheckboxRoot(props)
|
|
15
|
+
local _object = {
|
|
16
|
+
value = props.checked,
|
|
17
|
+
}
|
|
18
|
+
local _left = "defaultValue"
|
|
19
|
+
local _condition = props.defaultChecked
|
|
20
|
+
if _condition == nil then
|
|
21
|
+
_condition = false
|
|
22
|
+
end
|
|
23
|
+
_object[_left] = _condition
|
|
24
|
+
_object.onChange = props.onCheckedChange
|
|
25
|
+
local _binding = useControllableState(_object)
|
|
26
|
+
local checked = _binding[1]
|
|
27
|
+
local setCheckedState = _binding[2]
|
|
28
|
+
local disabled = props.disabled == true
|
|
29
|
+
local required = props.required == true
|
|
30
|
+
local setChecked = React.useCallback(function(nextChecked)
|
|
31
|
+
if disabled then
|
|
32
|
+
return nil
|
|
33
|
+
end
|
|
34
|
+
setCheckedState(nextChecked)
|
|
35
|
+
end, { disabled, setCheckedState })
|
|
36
|
+
local toggle = React.useCallback(function()
|
|
37
|
+
if disabled then
|
|
38
|
+
return nil
|
|
39
|
+
end
|
|
40
|
+
setCheckedState(getNextCheckedState(checked))
|
|
41
|
+
end, { checked, disabled, setCheckedState })
|
|
42
|
+
local contextValue = React.useMemo(function()
|
|
43
|
+
return {
|
|
44
|
+
checked = checked,
|
|
45
|
+
setChecked = setChecked,
|
|
46
|
+
disabled = disabled,
|
|
47
|
+
required = required,
|
|
48
|
+
}
|
|
49
|
+
end, { checked, disabled, required, setChecked })
|
|
50
|
+
local child = props.children
|
|
51
|
+
return React.createElement(CheckboxContextProvider, {
|
|
52
|
+
value = contextValue,
|
|
53
|
+
}, if props.asChild then ((function()
|
|
54
|
+
if not React.isValidElement(child) then
|
|
55
|
+
error("[Checkbox] `asChild` requires a child element.")
|
|
56
|
+
end
|
|
57
|
+
return React.createElement(Slot, {
|
|
58
|
+
Active = not disabled,
|
|
59
|
+
Event = {
|
|
60
|
+
Activated = toggle,
|
|
61
|
+
},
|
|
62
|
+
Selectable = not disabled,
|
|
63
|
+
}, child)
|
|
64
|
+
end)()) else (React.createElement("textbutton", {
|
|
65
|
+
Active = not disabled,
|
|
66
|
+
AutoButtonColor = false,
|
|
67
|
+
BackgroundColor3 = if checked ~= false then Color3.fromRGB(88, 142, 255) else Color3.fromRGB(59, 66, 84),
|
|
68
|
+
BorderSizePixel = 0,
|
|
69
|
+
Event = {
|
|
70
|
+
Activated = toggle,
|
|
71
|
+
},
|
|
72
|
+
Selectable = not disabled,
|
|
73
|
+
Size = UDim2.fromOffset(160, 36),
|
|
74
|
+
Text = if checked == "indeterminate" then "Indeterminate" elseif checked then "Checked" else "Unchecked",
|
|
75
|
+
TextColor3 = if disabled then Color3.fromRGB(145, 152, 168) else Color3.fromRGB(240, 244, 252),
|
|
76
|
+
TextSize = 15,
|
|
77
|
+
}, child)))
|
|
78
|
+
end
|
|
79
|
+
return {
|
|
80
|
+
CheckboxRoot = CheckboxRoot,
|
|
81
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local TS = _G[script]
|
|
3
|
+
local createStrictContext = TS.import(script, TS.getModule(script, "@lattice-ui", "core").out).createStrictContext
|
|
4
|
+
local _binding = createStrictContext("Checkbox")
|
|
5
|
+
local CheckboxContextProvider = _binding[1]
|
|
6
|
+
local useCheckboxContext = _binding[2]
|
|
7
|
+
return {
|
|
8
|
+
CheckboxContextProvider = CheckboxContextProvider,
|
|
9
|
+
useCheckboxContext = useCheckboxContext,
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type React from "@rbxts/react";
|
|
2
|
+
export type CheckedState = boolean | "indeterminate";
|
|
3
|
+
export type CheckboxSetChecked = (checked: CheckedState) => void;
|
|
4
|
+
export type CheckboxContextValue = {
|
|
5
|
+
checked: CheckedState;
|
|
6
|
+
setChecked: CheckboxSetChecked;
|
|
7
|
+
disabled: boolean;
|
|
8
|
+
required: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type CheckboxProps = {
|
|
11
|
+
checked?: CheckedState;
|
|
12
|
+
defaultChecked?: CheckedState;
|
|
13
|
+
onCheckedChange?: (checked: CheckedState) => void;
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
required?: boolean;
|
|
16
|
+
asChild?: boolean;
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
};
|
|
19
|
+
export type CheckboxIndicatorProps = {
|
|
20
|
+
forceMount?: boolean;
|
|
21
|
+
asChild?: boolean;
|
|
22
|
+
children?: React.ReactNode;
|
|
23
|
+
};
|
package/out/index.d.ts
ADDED
package/out/init.luau
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
-- Compiled with roblox-ts v3.0.0
|
|
2
|
+
local TS = _G[script]
|
|
3
|
+
local exports = {}
|
|
4
|
+
exports.CheckboxIndicator = TS.import(script, script, "Checkbox", "CheckboxIndicator").CheckboxIndicator
|
|
5
|
+
local _CheckboxRoot = TS.import(script, script, "Checkbox", "CheckboxRoot")
|
|
6
|
+
exports.CheckboxRoot = _CheckboxRoot.CheckboxRoot
|
|
7
|
+
exports.Checkbox = _CheckboxRoot.CheckboxRoot
|
|
8
|
+
return exports
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lattice-ui/checkbox",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "out/init.luau",
|
|
6
|
+
"types": "out/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@lattice-ui/core": "0.1.1"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@rbxts/react": "17.3.7-ts.1",
|
|
12
|
+
"@rbxts/react-roblox": "17.3.7-ts.1"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@rbxts/react": "^17",
|
|
16
|
+
"@rbxts/react-roblox": "^17"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rbxtsc -p tsconfig.json",
|
|
20
|
+
"watch": "rbxtsc -p tsconfig.json -w",
|
|
21
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { React, Slot } from "@lattice-ui/core";
|
|
2
|
+
import { useCheckboxContext } from "./context";
|
|
3
|
+
import type { CheckboxIndicatorProps } from "./types";
|
|
4
|
+
|
|
5
|
+
export function CheckboxIndicator(props: CheckboxIndicatorProps) {
|
|
6
|
+
const checkboxContext = useCheckboxContext();
|
|
7
|
+
const visible = checkboxContext.checked !== false;
|
|
8
|
+
const forceMount = props.forceMount === true;
|
|
9
|
+
|
|
10
|
+
if (!visible && !forceMount) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const child = props.children;
|
|
15
|
+
|
|
16
|
+
if (props.asChild) {
|
|
17
|
+
if (!React.isValidElement(child)) {
|
|
18
|
+
error("[CheckboxIndicator] `asChild` requires a child element.");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return <Slot Visible={visible}>{child}</Slot>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<frame
|
|
26
|
+
BackgroundColor3={Color3.fromRGB(240, 244, 252)}
|
|
27
|
+
BorderSizePixel={0}
|
|
28
|
+
Size={UDim2.fromOffset(12, 12)}
|
|
29
|
+
Visible={visible}
|
|
30
|
+
>
|
|
31
|
+
{child}
|
|
32
|
+
</frame>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { React, Slot, useControllableState } from "@lattice-ui/core";
|
|
2
|
+
import { CheckboxContextProvider } from "./context";
|
|
3
|
+
import type { CheckboxProps, CheckedState } from "./types";
|
|
4
|
+
|
|
5
|
+
function getNextCheckedState(checked: CheckedState) {
|
|
6
|
+
if (checked === "indeterminate") {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return !checked;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function CheckboxRoot(props: CheckboxProps) {
|
|
14
|
+
const [checked, setCheckedState] = useControllableState<CheckedState>({
|
|
15
|
+
value: props.checked,
|
|
16
|
+
defaultValue: props.defaultChecked ?? false,
|
|
17
|
+
onChange: props.onCheckedChange,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const disabled = props.disabled === true;
|
|
21
|
+
const required = props.required === true;
|
|
22
|
+
|
|
23
|
+
const setChecked = React.useCallback(
|
|
24
|
+
(nextChecked: CheckedState) => {
|
|
25
|
+
if (disabled) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setCheckedState(nextChecked);
|
|
30
|
+
},
|
|
31
|
+
[disabled, setCheckedState],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const toggle = React.useCallback(() => {
|
|
35
|
+
if (disabled) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setCheckedState(getNextCheckedState(checked));
|
|
40
|
+
}, [checked, disabled, setCheckedState]);
|
|
41
|
+
|
|
42
|
+
const contextValue = React.useMemo(
|
|
43
|
+
() => ({
|
|
44
|
+
checked,
|
|
45
|
+
setChecked,
|
|
46
|
+
disabled,
|
|
47
|
+
required,
|
|
48
|
+
}),
|
|
49
|
+
[checked, disabled, required, setChecked],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const child = props.children;
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<CheckboxContextProvider value={contextValue}>
|
|
56
|
+
{props.asChild ? (
|
|
57
|
+
(() => {
|
|
58
|
+
if (!React.isValidElement(child)) {
|
|
59
|
+
error("[Checkbox] `asChild` requires a child element.");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<Slot Active={!disabled} Event={{ Activated: toggle }} Selectable={!disabled}>
|
|
64
|
+
{child}
|
|
65
|
+
</Slot>
|
|
66
|
+
);
|
|
67
|
+
})()
|
|
68
|
+
) : (
|
|
69
|
+
<textbutton
|
|
70
|
+
Active={!disabled}
|
|
71
|
+
AutoButtonColor={false}
|
|
72
|
+
BackgroundColor3={checked !== false ? Color3.fromRGB(88, 142, 255) : Color3.fromRGB(59, 66, 84)}
|
|
73
|
+
BorderSizePixel={0}
|
|
74
|
+
Event={{ Activated: toggle }}
|
|
75
|
+
Selectable={!disabled}
|
|
76
|
+
Size={UDim2.fromOffset(160, 36)}
|
|
77
|
+
Text={checked === "indeterminate" ? "Indeterminate" : checked ? "Checked" : "Unchecked"}
|
|
78
|
+
TextColor3={disabled ? Color3.fromRGB(145, 152, 168) : Color3.fromRGB(240, 244, 252)}
|
|
79
|
+
TextSize={15}
|
|
80
|
+
>
|
|
81
|
+
{child}
|
|
82
|
+
</textbutton>
|
|
83
|
+
)}
|
|
84
|
+
</CheckboxContextProvider>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { createStrictContext } from "@lattice-ui/core";
|
|
2
|
+
import type { CheckboxContextValue } from "./types";
|
|
3
|
+
|
|
4
|
+
const [CheckboxContextProvider, useCheckboxContext] = createStrictContext<CheckboxContextValue>("Checkbox");
|
|
5
|
+
|
|
6
|
+
export { CheckboxContextProvider, useCheckboxContext };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type React from "@rbxts/react";
|
|
2
|
+
|
|
3
|
+
export type CheckedState = boolean | "indeterminate";
|
|
4
|
+
|
|
5
|
+
export type CheckboxSetChecked = (checked: CheckedState) => void;
|
|
6
|
+
|
|
7
|
+
export type CheckboxContextValue = {
|
|
8
|
+
checked: CheckedState;
|
|
9
|
+
setChecked: CheckboxSetChecked;
|
|
10
|
+
disabled: boolean;
|
|
11
|
+
required: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type CheckboxProps = {
|
|
15
|
+
checked?: CheckedState;
|
|
16
|
+
defaultChecked?: CheckedState;
|
|
17
|
+
onCheckedChange?: (checked: CheckedState) => void;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
required?: boolean;
|
|
20
|
+
asChild?: boolean;
|
|
21
|
+
children?: React.ReactNode;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type CheckboxIndicatorProps = {
|
|
25
|
+
forceMount?: boolean;
|
|
26
|
+
asChild?: boolean;
|
|
27
|
+
children?: React.ReactNode;
|
|
28
|
+
};
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|