Skip to content

Commit 8b72106

Browse files
Merge pull request #118 from christopher-buss/feature/react-components
refactor(ui): update component templates
2 parents bca893d + db0cc5a commit 8b72106

File tree

11 files changed

+86
-43
lines changed

11 files changed

+86
-43
lines changed

src/client/ui/components/error-boundary.ts src/client/ui/components/error-handler/error-boundary.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ErrorInfo } from "@rbxts/react";
33
import React, { ReactComponent } from "@rbxts/react";
44

55
interface ErrorBoundaryProps extends React.PropsWithChildren {
6-
readonly fallback: (error: unknown) => React.Element;
6+
readonly Fallback: (error: unknown) => React.Element;
77
}
88

99
interface ErrorBoundaryState {
@@ -42,10 +42,10 @@ export default class ErrorBoundary extends React.Component<ErrorBoundaryProps, E
4242

4343
public render(): React.ReactNode {
4444
const { hasError, message } = this.state;
45-
const { fallback, children } = this.props;
45+
const { Fallback, children } = this.props;
4646

4747
if (hasError) {
48-
return fallback(message);
48+
return Fallback(message);
4949
}
5050

5151
return children;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "@rbxts/react";
2+
3+
import ErrorBoundary from "./error-boundary";
4+
import ErrorPage from "./error-page";
5+
6+
type ErrorHandlerProps = Readonly<React.PropsWithChildren>;
7+
8+
export default function ErrorHandler({ children }: ErrorHandlerProps): React.Element {
9+
return (
10+
<ErrorBoundary
11+
Fallback={err => {
12+
return <ErrorPage Message={tostring(err)} />;
13+
}}
14+
>
15+
{children}
16+
</ErrorBoundary>
17+
);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "@rbxts/react";
2+
3+
interface ErrorPageProps {
4+
readonly Message: string;
5+
}
6+
7+
export default function ErrorPage({ Message }: ErrorPageProps): React.Element {
8+
warn(Message);
9+
10+
// eslint-disable-next-line react/no-useless-fragment -- This is a placeholder for future functions.
11+
return <></>;
12+
}

src/client/ui/components/primitive/button.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ export default function Button({
7676
};
7777

7878
return (
79-
<textbutton {...Native} Event={event}>
80-
{children}
79+
<textbutton
80+
{...Native}
81+
AnchorPoint={Native?.AnchorPoint ?? new Vector2(0.5, 0.5)}
82+
Event={event}
83+
Position={Native?.Position ?? new UDim2(0.5, 0, 0.5, 0)}
84+
Text={Native?.Text ?? ""}
85+
>
8186
{CornerRadius ? <uicorner CornerRadius={CornerRadius} /> : undefined}
87+
{children}
8288
</textbutton>
8389
);
8490
}

src/client/ui/components/primitive/canvas-group.tsx

+12-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ export type CanvasGroupProps = FrameProps<CanvasGroup>;
2020
* @see https://developer.roblox.com/en-us/api-reference/class/CanvasGroup
2121
*/
2222
const CanvasGroup = forwardRef((props: CanvasGroupProps, ref: React.Ref<CanvasGroup>) => {
23+
const { CornerRadius, Native, children } = props;
24+
2325
return (
24-
<canvasgroup ref={ref} {...props.Native} BorderSizePixel={0}>
25-
{props.children}
26-
{props.CornerRadius ? (
27-
<uicorner key="corner" CornerRadius={props.CornerRadius} />
28-
) : undefined}
26+
<canvasgroup
27+
ref={ref}
28+
{...Native}
29+
AnchorPoint={Native?.AnchorPoint ?? new Vector2(0.5, 0.5)}
30+
BackgroundTransparency={Native?.BackgroundTransparency ?? 1}
31+
BorderSizePixel={0}
32+
Position={Native?.Position ?? new UDim2(0.5, 0, 0.5, 0)}
33+
>
34+
{CornerRadius ? <uicorner key="corner" CornerRadius={CornerRadius} /> : undefined}
35+
{children}
2936
</canvasgroup>
3037
);
3138
});
+14-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import React, { forwardRef } from "@rbxts/react";
22

33
interface GroupProps extends React.PropsWithChildren {
4-
/**
5-
* The background transparency property exists purely for debugging
6-
* purposes. If you need a visible background, use a `Frame` instead.
7-
*
8-
* @ignore
9-
* @deprecated
10-
*/
11-
readonly BackgroundTransparency?: number;
124
/** All the default properties of a `Frame` component. */
135
readonly Native?: Partial<Omit<React.InstanceProps<Frame>, "BackgroundTransparency">>;
146
}
@@ -30,23 +22,20 @@ interface GroupProps extends React.PropsWithChildren {
3022
*
3123
* @component
3224
*/
33-
const Group = forwardRef(
34-
({ BackgroundTransparency, Native, children }: GroupProps, ref: React.Ref<Frame>) => {
35-
const { AnchorPoint, Position, Size } = Native ?? {};
25+
const Group = forwardRef(({ Native, children }: GroupProps, ref: React.Ref<Frame>) => {
26+
const { AnchorPoint, Position, Size } = Native ?? {};
3627

37-
return (
38-
<frame
39-
ref={ref}
40-
{...Native}
41-
AnchorPoint={AnchorPoint ?? new Vector2(0.5, 0.5)}
42-
BackgroundTransparency={BackgroundTransparency ?? 1}
43-
Position={Position ?? new UDim2(0.5, 0, 0.5, 0)}
44-
Size={Size ?? new UDim2(1, 0, 1, 0)}
45-
>
46-
{children}
47-
</frame>
48-
);
49-
},
50-
);
28+
return (
29+
<frame
30+
ref={ref}
31+
{...Native}
32+
AnchorPoint={AnchorPoint ?? new Vector2(0.5, 0.5)}
33+
Position={Position ?? new UDim2(0.5, 0, 0.5, 0)}
34+
Size={Size ?? new UDim2(1, 0, 1, 0)}
35+
>
36+
{children}
37+
</frame>
38+
);
39+
});
5140

5241
export default Group;

src/client/ui/components/primitive/image.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface ImageProps extends FrameProps<ImageLabel> {
1616
* @example
1717
*
1818
* ```tsx
19-
* <Image
19+
* <ImageLabel
2020
* Image="rbxassetid://1234567890"
2121
* Native={{
2222
* Size={new UDim2(0, 100, 0, 100)}
@@ -28,14 +28,22 @@ export interface ImageProps extends FrameProps<ImageLabel> {
2828
*
2929
* @see https://developer.roblox.com/en-us/api-reference/class/ImageLabel
3030
*/
31-
const Image = forwardRef((props: ImageProps, ref: React.Ref<ImageLabel>) => {
32-
const { CornerRadius, Native } = props;
31+
const ImageLabel = forwardRef((props: ImageProps, ref: React.Ref<ImageLabel>) => {
32+
const { CornerRadius, Image, Native, children } = props;
3333

3434
return (
35-
<imagelabel ref={ref} {...Native} BackgroundTransparency={1} Image={props.Image}>
35+
<imagelabel
36+
ref={ref}
37+
{...Native}
38+
AnchorPoint={Native?.AnchorPoint ?? new Vector2(0.5, 0.5)}
39+
BackgroundTransparency={Native?.BackgroundTransparency ?? 1}
40+
Image={Image}
41+
Position={Native?.Position ?? new UDim2(0.5, 0, 0.5, 0)}
42+
>
3643
{CornerRadius ? <uicorner CornerRadius={CornerRadius} /> : undefined}
44+
{children}
3745
</imagelabel>
3846
);
3947
});
4048

41-
export default Image;
49+
export default ImageLabel;

src/client/ui/components/primitive/layer.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "@rbxts/react";
22

3+
import { $NODE_ENV } from "rbxts-transform-env";
34
import { IS_EDIT } from "shared/constants";
45

56
import UltraWideContainer from "../ultra-wide-container";
@@ -47,7 +48,7 @@ export default function Layer({
4748
DisplayOrder,
4849
children,
4950
}: LayerProps): React.Element {
50-
return IS_EDIT ? (
51+
return $NODE_ENV === "development" && IS_EDIT ? (
5152
<Group
5253
Native={{
5354
AnchorPoint: new Vector2(0, 0),

src/client/ui/components/primitive/scrolling-frame.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ export default function ScrollingFrame({
5050
return (
5151
<scrollingframe
5252
{...Native}
53+
AnchorPoint={Native?.AnchorPoint ?? new Vector2(0.5, 0.5)}
5354
BackgroundTransparency={0}
5455
BorderSizePixel={0}
5556
CanvasSize={UDim2.fromOffset(
5657
CanvasSize.X !== 0 ? CanvasSize.X + 5 : 0,
5758
CanvasSize.Y !== 0 ? CanvasSize.Y + 5 : 0,
5859
)}
60+
Position={Native?.Position ?? new UDim2(0.5, 0, 0.5, 0)}
5961
ScrollBarImageColor3={theme.colors.secondary}
6062
ScrollBarThickness={rem(0.5)}
6163
>

src/types/util/react.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type BindingValue<T> = React.Binding<T> | T;
1+
export type BindingValue<T = unknown> = NonNullable<T> | React.Binding<T>;

src/types/util/roblox.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export type AssetId = `rbxassetid://${number}`;
1+
export type AssetId = `rbxassetid://${number}` | `rbxgameasset://${string}`;

0 commit comments

Comments
 (0)