Skip to content

Commit

Permalink
chore: add backgroundColor to setAttrs api
Browse files Browse the repository at this point in the history
  • Loading branch information
enijar committed May 5, 2023
1 parent 6e772f5 commit 3fb27ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/components/layer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function Layer(
const [attrs, setAttrs] = React.useState<Attrs>(() => {
return {
opacity,
backgroundColor,
};
});

Expand All @@ -91,6 +92,12 @@ function Layer(
});
}, [opacity]);

React.useEffect(() => {
setAttrs((attrs) => {
return { ...attrs, backgroundColor };
});
}, [backgroundColor]);

const xrUiContext = React.useContext(XrUiContext);

const res = React.useMemo(() => {
Expand All @@ -106,7 +113,10 @@ function Layer(
}, [optimizedRendering, xrUiContext.optimizedRendering]);

const groupRef = React.useRef<THREE.Group>(null);
const meshRef = React.useRef<THREE.Mesh<THREE.PlaneGeometry, THREE.MeshBasicMaterial>>(null);
const meshRef =
React.useRef<THREE.Mesh<THREE.PlaneGeometry, THREE.MeshBasicMaterial>>(
null
);
const materialRef = React.useRef<THREE.MeshBasicMaterial>(null);
const childrenGroupRef = React.useRef<THREE.Group>(null);

Expand Down Expand Up @@ -206,13 +216,15 @@ function Layer(

const layoutOnly = React.useMemo<boolean>(() => {
const props = [
backgroundColor === "transparent" ? undefined : backgroundColor,
attrs.backgroundColor === "transparent"
? undefined
: attrs.backgroundColor,
backgroundImage,
borderColor === "transparent" ? undefined : borderColor,
textContent,
];
return props.filter((prop) => prop !== undefined).length === 0;
}, [backgroundColor, backgroundImage, borderColor, textContent]);
}, [attrs.backgroundColor, backgroundImage, borderColor, textContent]);

// Create 2d canvas context
const ctx = React.useMemo<CanvasRenderingContext2D | null>(() => {
Expand Down Expand Up @@ -333,7 +345,6 @@ function Layer(
borderColor,
attrs,
images,
backgroundColor,
backgroundPosition,
backgroundSize,
font,
Expand Down Expand Up @@ -388,7 +399,7 @@ function Layer(
ctx.globalAlpha = attrs.opacity;

// Background color
ctx.fillStyle = backgroundColor;
ctx.fillStyle = attrs.backgroundColor;
ctx.lineWidth = borderWidth * res * 2;
ctx.fill();

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type LayerProps = GroupProps & {

export type Attrs = {
opacity: number;
backgroundColor: string;
};

export type LayerRef = {
Expand Down

0 comments on commit 3fb27ac

Please sign in to comment.