@lattice-ui/style 0.1.1 → 0.3.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/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 astra-void
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@lattice-ui/style",
3
- "version": "0.1.1",
3
+ "version": "0.3.1",
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"
12
+ "@lattice-ui/core": "0.3.1"
9
13
  },
10
14
  "devDependencies": {
11
15
  "@rbxts/react": "17.3.7-ts.1",
@@ -17,7 +21,7 @@
17
21
  },
18
22
  "scripts": {
19
23
  "build": "rbxtsc -p tsconfig.json",
20
- "watch": "rbxtsc -p tsconfig.json -w",
21
- "typecheck": "tsc -p tsconfig.typecheck.json"
24
+ "typecheck": "tsc -p tsconfig.typecheck.json",
25
+ "watch": "rbxtsc -p tsconfig.json -w"
22
26
  }
23
27
  }
package/src/index.ts DELETED
@@ -1,20 +0,0 @@
1
- export { Box } from "./primitives/Box";
2
- export { Text } from "./primitives/Text";
3
- export type { RecipeConfig, RecipeSelection, RecipeVariants } from "./recipe/createRecipe";
4
- export { createRecipe } from "./recipe/createRecipe";
5
- export { mergeGuiProps } from "./sx/mergeGuiProps";
6
- export type { Sx } from "./sx/sx";
7
- export { mergeSx, resolveSx } from "./sx/sx";
8
- export { ThemeProvider, useTheme, useThemeValue } from "./theme/ThemeProvider";
9
- export { createTheme, defaultDarkTheme, defaultLightTheme } from "./theme/tokens";
10
- export type {
11
- PartialTheme,
12
- Theme,
13
- ThemeColors,
14
- ThemeContextValue,
15
- ThemeProviderProps,
16
- ThemeRadius,
17
- ThemeSpace,
18
- ThemeTypography,
19
- ThemeTypographyStyle,
20
- } from "./theme/types";
@@ -1,44 +0,0 @@
1
- import { React, Slot } from "@lattice-ui/core";
2
- import { mergeGuiProps } from "../sx/mergeGuiProps";
3
- import type { Sx } from "../sx/sx";
4
- import { resolveSx } from "../sx/sx";
5
- import { useTheme } from "../theme/ThemeProvider";
6
-
7
- type StyleProps = React.Attributes & Record<string, unknown>;
8
-
9
- export type BoxProps = {
10
- asChild?: boolean;
11
- sx?: Sx<StyleProps>;
12
- children?: React.ReactNode;
13
- } & StyleProps;
14
-
15
- export function Box(props: BoxProps) {
16
- const asChild = props.asChild;
17
- const sx = props.sx;
18
- const children = props.children;
19
- const restProps: StyleProps = {};
20
- for (const [rawKey, value] of pairs(props as Record<string, unknown>)) {
21
- if (!typeIs(rawKey, "string")) {
22
- continue;
23
- }
24
-
25
- if (rawKey === "asChild" || rawKey === "sx" || rawKey === "children") {
26
- continue;
27
- }
28
-
29
- restProps[rawKey] = value;
30
- }
31
-
32
- const { theme } = useTheme();
33
- const mergedProps = mergeGuiProps(restProps, resolveSx(sx, theme));
34
-
35
- if (asChild) {
36
- if (!React.isValidElement(children)) {
37
- error("[Box] `asChild` requires a single child element.");
38
- }
39
-
40
- return <Slot {...mergedProps}>{children}</Slot>;
41
- }
42
-
43
- return React.createElement("frame", mergedProps as never, children);
44
- }
@@ -1,44 +0,0 @@
1
- import { React, Slot } from "@lattice-ui/core";
2
- import { mergeGuiProps } from "../sx/mergeGuiProps";
3
- import type { Sx } from "../sx/sx";
4
- import { resolveSx } from "../sx/sx";
5
- import { useTheme } from "../theme/ThemeProvider";
6
-
7
- type StyleProps = React.Attributes & Record<string, unknown>;
8
-
9
- export type TextProps = {
10
- asChild?: boolean;
11
- sx?: Sx<StyleProps>;
12
- children?: React.ReactNode;
13
- } & StyleProps;
14
-
15
- export function Text(props: TextProps) {
16
- const asChild = props.asChild;
17
- const sx = props.sx;
18
- const children = props.children;
19
- const restProps: StyleProps = {};
20
- for (const [rawKey, value] of pairs(props as Record<string, unknown>)) {
21
- if (!typeIs(rawKey, "string")) {
22
- continue;
23
- }
24
-
25
- if (rawKey === "asChild" || rawKey === "sx" || rawKey === "children") {
26
- continue;
27
- }
28
-
29
- restProps[rawKey] = value;
30
- }
31
-
32
- const { theme } = useTheme();
33
- const mergedProps = mergeGuiProps(restProps, resolveSx(sx, theme));
34
-
35
- if (asChild) {
36
- if (!React.isValidElement(children)) {
37
- error("[Text] `asChild` requires a single child element.");
38
- }
39
-
40
- return <Slot {...mergedProps}>{children}</Slot>;
41
- }
42
-
43
- return React.createElement("textlabel", mergedProps as never, children);
44
- }
@@ -1,85 +0,0 @@
1
- import { mergeGuiProps } from "../sx/mergeGuiProps";
2
- import type { Sx } from "../sx/sx";
3
- import { resolveSx } from "../sx/sx";
4
- import type { Theme } from "../theme/types";
5
-
6
- type GuiPropRecord = Record<string, unknown>;
7
-
8
- export type RecipeVariants<Props extends GuiPropRecord> = Record<string, Record<string, Sx<Props>>>;
9
- export type RecipeSelection<Variants extends RecipeVariants<GuiPropRecord>> = Partial<
10
- Record<keyof Variants & string, string>
11
- >;
12
-
13
- export type RecipeConfig<Props extends GuiPropRecord, Variants extends RecipeVariants<Props>> = {
14
- base?: Sx<Props>;
15
- variants?: Variants;
16
- defaultVariants?: RecipeSelection<Variants>;
17
- compoundVariants?: Array<{
18
- variants: RecipeSelection<Variants>;
19
- sx: Sx<Props>;
20
- }>;
21
- };
22
-
23
- function isCompoundMatch<Variants extends RecipeVariants<GuiPropRecord>>(
24
- candidate: RecipeSelection<Variants>,
25
- resolvedSelection: RecipeSelection<Variants>,
26
- ) {
27
- const candidateRecord = candidate as Record<string, string | undefined>;
28
- const resolvedRecord = resolvedSelection as Record<string, string | undefined>;
29
-
30
- for (const [rawVariantName, rawExpectedValue] of pairs(candidateRecord)) {
31
- if (!typeIs(rawVariantName, "string") || !typeIs(rawExpectedValue, "string")) {
32
- continue;
33
- }
34
-
35
- const actualValue = resolvedRecord[rawVariantName];
36
- if (actualValue !== rawExpectedValue) {
37
- return false;
38
- }
39
- }
40
-
41
- return true;
42
- }
43
-
44
- export function createRecipe<Props extends GuiPropRecord, Variants extends RecipeVariants<Props>>(
45
- config: RecipeConfig<Props, Variants>,
46
- ) {
47
- return (selection: RecipeSelection<Variants> | undefined, theme: Theme): Partial<Props> => {
48
- const resolvedSelection = {
49
- ...(config.defaultVariants ?? {}),
50
- ...(selection ?? {}),
51
- } as RecipeSelection<Variants>;
52
-
53
- let merged = resolveSx(config.base, theme);
54
-
55
- const variants = config.variants;
56
- if (variants) {
57
- const variantsRecord = variants as Record<string, Record<string, Sx<Props>>>;
58
- const resolvedRecord = resolvedSelection as Record<string, string | undefined>;
59
-
60
- for (const [rawVariantName, rawVariantMap] of pairs(variantsRecord)) {
61
- if (!typeIs(rawVariantName, "string") || !typeIs(rawVariantMap, "table")) {
62
- continue;
63
- }
64
-
65
- const selectedValue = resolvedRecord[rawVariantName];
66
- if (selectedValue === undefined) {
67
- continue;
68
- }
69
-
70
- const sx = rawVariantMap[selectedValue];
71
- merged = mergeGuiProps(merged, resolveSx(sx, theme));
72
- }
73
- }
74
-
75
- for (const compound of config.compoundVariants ?? []) {
76
- if (!isCompoundMatch(compound.variants, resolvedSelection)) {
77
- continue;
78
- }
79
-
80
- merged = mergeGuiProps(merged, resolveSx(compound.sx, theme));
81
- }
82
-
83
- return merged;
84
- };
85
- }
@@ -1,87 +0,0 @@
1
- type GuiHandler = (...args: unknown[]) => void;
2
- type GuiHandlerTable = Partial<Record<string, GuiHandler>>;
3
- type GuiPropRecord = Record<string, unknown>;
4
-
5
- function isRecord(value: unknown): value is GuiPropRecord {
6
- return typeIs(value, "table");
7
- }
8
-
9
- function isFn(value: unknown): value is GuiHandler {
10
- return typeIs(value, "function");
11
- }
12
-
13
- function toHandlerTable(value: unknown): GuiHandlerTable | undefined {
14
- if (!isRecord(value)) {
15
- return undefined;
16
- }
17
-
18
- const out: GuiHandlerTable = {};
19
- for (const [rawKey, candidate] of pairs(value)) {
20
- if (!typeIs(rawKey, "string") || !isFn(candidate)) {
21
- continue;
22
- }
23
-
24
- out[rawKey] = candidate;
25
- }
26
-
27
- return next(out)[0] !== undefined ? out : undefined;
28
- }
29
-
30
- function mergeHandlerTables(tables: Array<GuiHandlerTable | undefined>): GuiHandlerTable | undefined {
31
- const out: GuiHandlerTable = {};
32
-
33
- for (const handlerTable of tables) {
34
- if (!handlerTable) {
35
- continue;
36
- }
37
-
38
- for (const [rawKey, candidate] of pairs(handlerTable)) {
39
- if (!typeIs(rawKey, "string") || !isFn(candidate)) {
40
- continue;
41
- }
42
-
43
- const previous = out[rawKey];
44
- out[rawKey] =
45
- previous !== undefined
46
- ? (...args: unknown[]) => {
47
- previous(...args);
48
- candidate(...args);
49
- }
50
- : candidate;
51
- }
52
- }
53
-
54
- return next(out)[0] !== undefined ? out : undefined;
55
- }
56
-
57
- export function mergeGuiProps<Props extends GuiPropRecord>(
58
- base?: Partial<Props>,
59
- variant?: Partial<Props>,
60
- user?: Partial<Props>,
61
- ): Partial<Props> {
62
- const merged = {
63
- ...(base ?? {}),
64
- ...(variant ?? {}),
65
- ...(user ?? {}),
66
- } as Partial<Props>;
67
-
68
- const mergedEvent = mergeHandlerTables([
69
- toHandlerTable(base?.Event),
70
- toHandlerTable(variant?.Event),
71
- toHandlerTable(user?.Event),
72
- ]);
73
- if (mergedEvent) {
74
- (merged as GuiPropRecord).Event = mergedEvent;
75
- }
76
-
77
- const mergedChange = mergeHandlerTables([
78
- toHandlerTable(base?.Change),
79
- toHandlerTable(variant?.Change),
80
- toHandlerTable(user?.Change),
81
- ]);
82
- if (mergedChange) {
83
- (merged as GuiPropRecord).Change = mergedChange;
84
- }
85
-
86
- return merged;
87
- }
package/src/sx/sx.ts DELETED
@@ -1,35 +0,0 @@
1
- import type { Theme } from "../theme/types";
2
- import { mergeGuiProps } from "./mergeGuiProps";
3
-
4
- type GuiPropRecord = Record<string, unknown>;
5
-
6
- export type Sx<Props extends GuiPropRecord> = Partial<Props> | ((theme: Theme) => Partial<Props>) | undefined;
7
-
8
- function isSxResolver<Props extends GuiPropRecord>(sx: Sx<Props>): sx is (theme: Theme) => Partial<Props> {
9
- return typeIs(sx, "function");
10
- }
11
-
12
- export function resolveSx<Props extends GuiPropRecord>(sx: Sx<Props>, theme: Theme): Partial<Props> {
13
- if (!sx) {
14
- return {};
15
- }
16
-
17
- if (isSxResolver(sx)) {
18
- return sx(theme);
19
- }
20
-
21
- return sx;
22
- }
23
-
24
- export function mergeSx<Props extends GuiPropRecord>(...sxList: Array<Sx<Props>>): Sx<Props> {
25
- return (theme) => {
26
- let merged: Partial<Props> = {};
27
-
28
- for (const sx of sxList) {
29
- const resolved = resolveSx(sx, theme);
30
- merged = mergeGuiProps(merged, resolved);
31
- }
32
-
33
- return merged;
34
- };
35
- }
@@ -1,41 +0,0 @@
1
- import { createStrictContext, React } from "@lattice-ui/core";
2
- import { defaultLightTheme } from "./tokens";
3
- import type { Theme, ThemeContextValue, ThemeProviderProps } from "./types";
4
-
5
- const [ThemeContextProvider, useThemeContext] = createStrictContext<ThemeContextValue>("ThemeProvider");
6
-
7
- export function ThemeProvider(props: ThemeProviderProps) {
8
- const [internalTheme, setInternalTheme] = React.useState(props.defaultTheme ?? defaultLightTheme);
9
- const controlled = props.theme !== undefined;
10
- const resolvedTheme = props.theme ?? internalTheme;
11
-
12
- const setTheme = React.useCallback(
13
- (nextTheme: Theme) => {
14
- if (!controlled) {
15
- setInternalTheme(nextTheme);
16
- }
17
-
18
- props.onThemeChange?.(nextTheme);
19
- },
20
- [controlled, props.onThemeChange],
21
- );
22
-
23
- const contextValue = React.useMemo(
24
- () => ({
25
- theme: resolvedTheme,
26
- setTheme,
27
- }),
28
- [resolvedTheme, setTheme],
29
- );
30
-
31
- return <ThemeContextProvider value={contextValue}>{props.children}</ThemeContextProvider>;
32
- }
33
-
34
- export function useTheme() {
35
- return useThemeContext();
36
- }
37
-
38
- export function useThemeValue<T>(selector: (theme: Theme) => T): T {
39
- const context = useThemeContext();
40
- return React.useMemo(() => selector(context.theme), [context.theme, selector]);
41
- }
@@ -1,112 +0,0 @@
1
- import type { PartialTheme, Theme } from "./types";
2
-
3
- const defaultSpace = {
4
- 0: 0,
5
- 2: 2,
6
- 4: 4,
7
- 6: 6,
8
- 8: 8,
9
- 10: 10,
10
- 12: 12,
11
- 14: 14,
12
- 16: 16,
13
- 20: 20,
14
- 24: 24,
15
- 32: 32,
16
- };
17
-
18
- const defaultRadius = {
19
- none: 0,
20
- sm: 4,
21
- md: 8,
22
- lg: 12,
23
- xl: 16,
24
- full: 999,
25
- };
26
-
27
- const defaultTypography = {
28
- labelSm: {
29
- font: Enum.Font.Gotham,
30
- textSize: 14,
31
- },
32
- bodyMd: {
33
- font: Enum.Font.Gotham,
34
- textSize: 16,
35
- },
36
- titleMd: {
37
- font: Enum.Font.GothamBold,
38
- textSize: 22,
39
- },
40
- };
41
-
42
- export const defaultLightTheme: Theme = {
43
- colors: {
44
- background: Color3.fromRGB(246, 249, 252),
45
- surface: Color3.fromRGB(233, 239, 246),
46
- surfaceElevated: Color3.fromRGB(255, 255, 255),
47
- border: Color3.fromRGB(193, 202, 214),
48
- textPrimary: Color3.fromRGB(27, 33, 44),
49
- textSecondary: Color3.fromRGB(79, 90, 107),
50
- accent: Color3.fromRGB(46, 114, 216),
51
- accentContrast: Color3.fromRGB(239, 245, 252),
52
- danger: Color3.fromRGB(167, 56, 64),
53
- dangerContrast: Color3.fromRGB(254, 236, 238),
54
- overlay: Color3.fromRGB(12, 16, 23),
55
- },
56
- space: defaultSpace,
57
- radius: defaultRadius,
58
- typography: defaultTypography,
59
- };
60
-
61
- export const defaultDarkTheme: Theme = {
62
- colors: {
63
- background: Color3.fromRGB(18, 21, 26),
64
- surface: Color3.fromRGB(32, 37, 46),
65
- surfaceElevated: Color3.fromRGB(40, 47, 60),
66
- border: Color3.fromRGB(72, 80, 98),
67
- textPrimary: Color3.fromRGB(233, 239, 246),
68
- textSecondary: Color3.fromRGB(176, 186, 201),
69
- accent: Color3.fromRGB(43, 105, 196),
70
- accentContrast: Color3.fromRGB(240, 244, 250),
71
- danger: Color3.fromRGB(129, 57, 63),
72
- dangerContrast: Color3.fromRGB(245, 223, 226),
73
- overlay: Color3.fromRGB(8, 10, 14),
74
- },
75
- space: defaultSpace,
76
- radius: defaultRadius,
77
- typography: defaultTypography,
78
- };
79
-
80
- function mergeTheme(baseTheme: Theme, partialTheme?: PartialTheme): Theme {
81
- if (!partialTheme) {
82
- return {
83
- colors: { ...baseTheme.colors },
84
- space: { ...baseTheme.space },
85
- radius: { ...baseTheme.radius },
86
- typography: { ...baseTheme.typography },
87
- };
88
- }
89
-
90
- return {
91
- colors: {
92
- ...baseTheme.colors,
93
- ...(partialTheme.colors ?? {}),
94
- },
95
- space: {
96
- ...baseTheme.space,
97
- ...(partialTheme.space ?? {}),
98
- },
99
- radius: {
100
- ...baseTheme.radius,
101
- ...(partialTheme.radius ?? {}),
102
- },
103
- typography: {
104
- ...baseTheme.typography,
105
- ...(partialTheme.typography ?? {}),
106
- },
107
- };
108
- }
109
-
110
- export function createTheme(partialTheme?: PartialTheme) {
111
- return mergeTheme(defaultLightTheme, partialTheme);
112
- }
@@ -1,76 +0,0 @@
1
- import type React from "@rbxts/react";
2
-
3
- export type ThemeColors = {
4
- background: Color3;
5
- surface: Color3;
6
- surfaceElevated: Color3;
7
- border: Color3;
8
- textPrimary: Color3;
9
- textSecondary: Color3;
10
- accent: Color3;
11
- accentContrast: Color3;
12
- danger: Color3;
13
- dangerContrast: Color3;
14
- overlay: Color3;
15
- };
16
-
17
- export type ThemeSpace = {
18
- 0: number;
19
- 2: number;
20
- 4: number;
21
- 6: number;
22
- 8: number;
23
- 10: number;
24
- 12: number;
25
- 14: number;
26
- 16: number;
27
- 20: number;
28
- 24: number;
29
- 32: number;
30
- };
31
-
32
- export type ThemeRadius = {
33
- none: number;
34
- sm: number;
35
- md: number;
36
- lg: number;
37
- xl: number;
38
- full: number;
39
- };
40
-
41
- export type ThemeTypographyStyle = {
42
- font: Enum.Font;
43
- textSize: number;
44
- };
45
-
46
- export type ThemeTypography = {
47
- labelSm: ThemeTypographyStyle;
48
- bodyMd: ThemeTypographyStyle;
49
- titleMd: ThemeTypographyStyle;
50
- };
51
-
52
- export type Theme = {
53
- colors: ThemeColors;
54
- space: ThemeSpace;
55
- radius: ThemeRadius;
56
- typography: ThemeTypography;
57
- };
58
-
59
- export type PartialTheme = {
60
- colors?: Partial<ThemeColors>;
61
- space?: Partial<ThemeSpace>;
62
- radius?: Partial<ThemeRadius>;
63
- typography?: Partial<ThemeTypography>;
64
- };
65
-
66
- export type ThemeContextValue = {
67
- theme: Theme;
68
- setTheme: (nextTheme: Theme) => void;
69
- };
70
-
71
- export type ThemeProviderProps = {
72
- theme?: Theme;
73
- defaultTheme?: Theme;
74
- onThemeChange?: (nextTheme: Theme) => void;
75
- children?: React.ReactNode;
76
- };
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
- }