@lattice-ui/switch 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/Switch/SwitchRoot.d.ts +3 -0
- package/out/Switch/SwitchRoot.luau +73 -0
- package/out/Switch/SwitchThumb.d.ts +3 -0
- package/out/Switch/SwitchThumb.luau +32 -0
- package/out/Switch/context.d.ts +3 -0
- package/out/Switch/context.luau +10 -0
- package/out/Switch/types.d.ts +20 -0
- package/out/Switch/types.luau +2 -0
- package/out/index.d.ts +3 -0
- package/out/init.luau +8 -0
- package/package.json +23 -0
- package/src/Switch/SwitchRoot.tsx +76 -0
- package/src/Switch/SwitchThumb.tsx +34 -0
- package/src/Switch/context.ts +6 -0
- package/src/Switch/types.ts +24 -0
- package/src/index.ts +3 -0
- package/tsconfig.json +16 -0
- package/tsconfig.typecheck.json +25 -0
|
@@ -0,0 +1,73 @@
|
|
|
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 SwitchContextProvider = TS.import(script, script.Parent, "context").SwitchContextProvider
|
|
8
|
+
local function SwitchRoot(props)
|
|
9
|
+
local _object = {
|
|
10
|
+
value = props.checked,
|
|
11
|
+
}
|
|
12
|
+
local _left = "defaultValue"
|
|
13
|
+
local _condition = props.defaultChecked
|
|
14
|
+
if _condition == nil then
|
|
15
|
+
_condition = false
|
|
16
|
+
end
|
|
17
|
+
_object[_left] = _condition
|
|
18
|
+
_object.onChange = props.onCheckedChange
|
|
19
|
+
local _binding = useControllableState(_object)
|
|
20
|
+
local checked = _binding[1]
|
|
21
|
+
local setCheckedState = _binding[2]
|
|
22
|
+
local disabled = props.disabled == true
|
|
23
|
+
local setChecked = React.useCallback(function(nextChecked)
|
|
24
|
+
if disabled then
|
|
25
|
+
return nil
|
|
26
|
+
end
|
|
27
|
+
setCheckedState(nextChecked)
|
|
28
|
+
end, { disabled, setCheckedState })
|
|
29
|
+
local toggle = React.useCallback(function()
|
|
30
|
+
if disabled then
|
|
31
|
+
return nil
|
|
32
|
+
end
|
|
33
|
+
setCheckedState(not checked)
|
|
34
|
+
end, { checked, disabled, setCheckedState })
|
|
35
|
+
local contextValue = React.useMemo(function()
|
|
36
|
+
return {
|
|
37
|
+
checked = checked,
|
|
38
|
+
setChecked = setChecked,
|
|
39
|
+
disabled = disabled,
|
|
40
|
+
}
|
|
41
|
+
end, { checked, disabled, setChecked })
|
|
42
|
+
local child = props.children
|
|
43
|
+
return React.createElement(SwitchContextProvider, {
|
|
44
|
+
value = contextValue,
|
|
45
|
+
}, if props.asChild then ((function()
|
|
46
|
+
if not React.isValidElement(child) then
|
|
47
|
+
error("[Switch] `asChild` requires a child element.")
|
|
48
|
+
end
|
|
49
|
+
return React.createElement(Slot, {
|
|
50
|
+
Active = not disabled,
|
|
51
|
+
Event = {
|
|
52
|
+
Activated = toggle,
|
|
53
|
+
},
|
|
54
|
+
Selectable = not disabled,
|
|
55
|
+
}, child)
|
|
56
|
+
end)()) else (React.createElement("textbutton", {
|
|
57
|
+
Active = not disabled,
|
|
58
|
+
AutoButtonColor = false,
|
|
59
|
+
BackgroundColor3 = if checked then Color3.fromRGB(86, 141, 255) else Color3.fromRGB(66, 73, 91),
|
|
60
|
+
BorderSizePixel = 0,
|
|
61
|
+
Event = {
|
|
62
|
+
Activated = toggle,
|
|
63
|
+
},
|
|
64
|
+
Selectable = not disabled,
|
|
65
|
+
Size = UDim2.fromOffset(160, 36),
|
|
66
|
+
Text = if checked then "On" else "Off",
|
|
67
|
+
TextColor3 = if disabled then Color3.fromRGB(145, 152, 168) else Color3.fromRGB(240, 244, 252),
|
|
68
|
+
TextSize = 15,
|
|
69
|
+
}, child)))
|
|
70
|
+
end
|
|
71
|
+
return {
|
|
72
|
+
SwitchRoot = SwitchRoot,
|
|
73
|
+
}
|
|
@@ -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 useSwitchContext = TS.import(script, script.Parent, "context").useSwitchContext
|
|
7
|
+
local function SwitchThumb(props)
|
|
8
|
+
local switchContext = useSwitchContext()
|
|
9
|
+
local visible = switchContext.checked
|
|
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("[SwitchThumb] `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(16, 16),
|
|
27
|
+
Visible = visible,
|
|
28
|
+
}, child)
|
|
29
|
+
end
|
|
30
|
+
return {
|
|
31
|
+
SwitchThumb = SwitchThumb,
|
|
32
|
+
}
|
|
@@ -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("Switch")
|
|
5
|
+
local SwitchContextProvider = _binding[1]
|
|
6
|
+
local useSwitchContext = _binding[2]
|
|
7
|
+
return {
|
|
8
|
+
SwitchContextProvider = SwitchContextProvider,
|
|
9
|
+
useSwitchContext = useSwitchContext,
|
|
10
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type React from "@rbxts/react";
|
|
2
|
+
export type SwitchSetChecked = (checked: boolean) => void;
|
|
3
|
+
export type SwitchContextValue = {
|
|
4
|
+
checked: boolean;
|
|
5
|
+
setChecked: SwitchSetChecked;
|
|
6
|
+
disabled: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type SwitchProps = {
|
|
9
|
+
checked?: boolean;
|
|
10
|
+
defaultChecked?: boolean;
|
|
11
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
asChild?: boolean;
|
|
14
|
+
children?: React.ReactNode;
|
|
15
|
+
};
|
|
16
|
+
export type SwitchThumbProps = {
|
|
17
|
+
forceMount?: boolean;
|
|
18
|
+
asChild?: boolean;
|
|
19
|
+
children?: React.ReactNode;
|
|
20
|
+
};
|
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
|
+
local _SwitchRoot = TS.import(script, script, "Switch", "SwitchRoot")
|
|
5
|
+
exports.SwitchRoot = _SwitchRoot.SwitchRoot
|
|
6
|
+
exports.Switch = _SwitchRoot.SwitchRoot
|
|
7
|
+
exports.SwitchThumb = TS.import(script, script, "Switch", "SwitchThumb").SwitchThumb
|
|
8
|
+
return exports
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lattice-ui/switch",
|
|
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,76 @@
|
|
|
1
|
+
import { React, Slot, useControllableState } from "@lattice-ui/core";
|
|
2
|
+
import { SwitchContextProvider } from "./context";
|
|
3
|
+
import type { SwitchProps } from "./types";
|
|
4
|
+
|
|
5
|
+
export function SwitchRoot(props: SwitchProps) {
|
|
6
|
+
const [checked, setCheckedState] = useControllableState<boolean>({
|
|
7
|
+
value: props.checked,
|
|
8
|
+
defaultValue: props.defaultChecked ?? false,
|
|
9
|
+
onChange: props.onCheckedChange,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const disabled = props.disabled === true;
|
|
13
|
+
|
|
14
|
+
const setChecked = React.useCallback(
|
|
15
|
+
(nextChecked: boolean) => {
|
|
16
|
+
if (disabled) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
setCheckedState(nextChecked);
|
|
21
|
+
},
|
|
22
|
+
[disabled, setCheckedState],
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const toggle = React.useCallback(() => {
|
|
26
|
+
if (disabled) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setCheckedState(!checked);
|
|
31
|
+
}, [checked, disabled, setCheckedState]);
|
|
32
|
+
|
|
33
|
+
const contextValue = React.useMemo(
|
|
34
|
+
() => ({
|
|
35
|
+
checked,
|
|
36
|
+
setChecked,
|
|
37
|
+
disabled,
|
|
38
|
+
}),
|
|
39
|
+
[checked, disabled, setChecked],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const child = props.children;
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<SwitchContextProvider value={contextValue}>
|
|
46
|
+
{props.asChild ? (
|
|
47
|
+
(() => {
|
|
48
|
+
if (!React.isValidElement(child)) {
|
|
49
|
+
error("[Switch] `asChild` requires a child element.");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<Slot Active={!disabled} Event={{ Activated: toggle }} Selectable={!disabled}>
|
|
54
|
+
{child}
|
|
55
|
+
</Slot>
|
|
56
|
+
);
|
|
57
|
+
})()
|
|
58
|
+
) : (
|
|
59
|
+
<textbutton
|
|
60
|
+
Active={!disabled}
|
|
61
|
+
AutoButtonColor={false}
|
|
62
|
+
BackgroundColor3={checked ? Color3.fromRGB(86, 141, 255) : Color3.fromRGB(66, 73, 91)}
|
|
63
|
+
BorderSizePixel={0}
|
|
64
|
+
Event={{ Activated: toggle }}
|
|
65
|
+
Selectable={!disabled}
|
|
66
|
+
Size={UDim2.fromOffset(160, 36)}
|
|
67
|
+
Text={checked ? "On" : "Off"}
|
|
68
|
+
TextColor3={disabled ? Color3.fromRGB(145, 152, 168) : Color3.fromRGB(240, 244, 252)}
|
|
69
|
+
TextSize={15}
|
|
70
|
+
>
|
|
71
|
+
{child}
|
|
72
|
+
</textbutton>
|
|
73
|
+
)}
|
|
74
|
+
</SwitchContextProvider>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { React, Slot } from "@lattice-ui/core";
|
|
2
|
+
import { useSwitchContext } from "./context";
|
|
3
|
+
import type { SwitchThumbProps } from "./types";
|
|
4
|
+
|
|
5
|
+
export function SwitchThumb(props: SwitchThumbProps) {
|
|
6
|
+
const switchContext = useSwitchContext();
|
|
7
|
+
const visible = switchContext.checked;
|
|
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("[SwitchThumb] `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(16, 16)}
|
|
29
|
+
Visible={visible}
|
|
30
|
+
>
|
|
31
|
+
{child}
|
|
32
|
+
</frame>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type React from "@rbxts/react";
|
|
2
|
+
|
|
3
|
+
export type SwitchSetChecked = (checked: boolean) => void;
|
|
4
|
+
|
|
5
|
+
export type SwitchContextValue = {
|
|
6
|
+
checked: boolean;
|
|
7
|
+
setChecked: SwitchSetChecked;
|
|
8
|
+
disabled: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type SwitchProps = {
|
|
12
|
+
checked?: boolean;
|
|
13
|
+
defaultChecked?: boolean;
|
|
14
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
asChild?: boolean;
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type SwitchThumbProps = {
|
|
21
|
+
forceMount?: boolean;
|
|
22
|
+
asChild?: boolean;
|
|
23
|
+
children?: React.ReactNode;
|
|
24
|
+
};
|
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
|
+
}
|