Skip to content

Commit 4d01764

Browse files
Merge pull request #161 from elraccoone/improvements/refactor-width-height-to-style
Improvements/refactor width height to style
2 parents 7744fe5 + c9932fd commit 4d01764

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Welcome to the React Unity WebGL Documentation! My name is Jeffrey and I'm here
5656
- [Tracking the loading progression](#tracking-the-loading-progression)
5757
- [Handeling on when the Application is loaded](#handeling-on-when-the-application-is-loaded)
5858
- [Entering or Leaving Fullscreen](#entering-or-leaving-fullscreen)
59-
- [Setting the Canvas's Height and Width](#setting-the-canvass-height-and-width)
59+
- [Adding Styles to the Canvas Component](#adding-styles-to-the-canvas-component)
6060
- [Setting the Canvas's ClassName](#setting-the-canvass-classname)
6161
- [Device Pixel Ratio and Retina Support](#device-pixel-ratio-and-retina-support)
6262
- [Tab Index and Keyboard Capturing](#tab-index-and-keyboard-capturing)
@@ -397,16 +397,16 @@ const App = () => {
397397
};
398398
```
399399

400-
## Setting the Canvas's Height and Width
400+
## Adding Styles to the Canvas Component
401401

402-
> Available since version 5.6.0
402+
> Available since version 8.2.0
403403
404-
The default size is 800px width to 600px height. You can easily overrule them by passing the following props. The height and width properties are used to set the height and width of the wrapper element.
404+
The style tag allows for adding inline CSS for styling the component. The style's properties will be assigned directly onto the actual canvas.
405405

406-
The height and width can be set to auto (Means that the browser calculates the height and width), or be specified in length values, like px, cm, etc., or in percent of the containing block. Note that the height and width properties do not include padding, borders, or margins; they set the height/width of the area inside the padding, border, and margin of the element!
406+
The style attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM style JavaScript property, is more efficient, and prevents XSS security holes. React will automatically append a “px” suffix to certain numeric inline style properties. If you want to use units other than “px”, specify the value as a string with the desired unit.
407407

408408
```tsx
409-
<Unity height={number | string} width={number | string} />
409+
<Unity style={CSSProperties} />
410410
```
411411

412412
#### Example implementation
@@ -427,7 +427,17 @@ const unityContext = new UnityContext({
427427
});
428428

429429
const App = () => {
430-
return <Unity unityContext={unityContext} width="100%" height="950px" />;
430+
return (
431+
<Unity
432+
unityContext={unityContext}
433+
style={{
434+
height: "100%",
435+
width: 950,
436+
border: "2px solid black",
437+
background: "grey",
438+
}}
439+
/>
440+
);
431441
};
432442
```
433443

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-unity-webgl",
3-
"version": "8.1.7",
3+
"version": "8.2.0",
44
"description": "React Unity WebGL provides an easy solution for embedding Unity WebGL builds in your React application, with two-way communication between your React and Unity application with advanced API's.",
55
"keywords": [
66
"React",

source/components/unity.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,10 @@ export default class Unity extends PureComponent<IUnityProps, {}> {
8686
*/
8787
public render(): React.ReactNode {
8888
return createElement("canvas", {
89+
ref: (ref: HTMLCanvasElement) => (this.htmlCanvasElementReference = ref),
8990
className: this.props.className || "",
90-
tabIndex: this.props.tabIndex,
91-
ref: (r: HTMLCanvasElement) => (this.htmlCanvasElementReference = r),
92-
style: {
93-
width: this.props.width || "800px",
94-
height: this.props.height || "600px",
95-
},
91+
tabIndex: this.props.tabIndex || undefined,
92+
style: this.props.style || {},
9693
});
9794
}
9895
}

source/interfaces/unity-props.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1+
import { CSSProperties } from "react";
12
import UnityContext from "../models/unity-context";
23

34
export default interface IUnityProps {
45
/**
5-
* The context that should be rendered within the Unity component.
6+
* The Context which should be rendered be the Unity Component.
67
* @type {UnityContext}
78
*/
89
unityContext: UnityContext;
910

1011
/**
11-
* The class name for the canvas wrapper.
12+
* The Class Name will be applied to the Canvas.
1213
* @type {string}
1314
*/
1415
className?: string;
1516

1617
/**
17-
* The width of the element.
18-
* @type {string | number}
18+
* The styles will be applied to the Canvas.
19+
* @type {CSSProperties}
1920
*/
20-
width?: string | number;
21-
22-
/**
23-
* The height of the element.
24-
* @type {string | number}
25-
*/
26-
height?: string | number;
21+
style?: CSSProperties;
2722

2823
/**
2924
* The tabIndex of the element. Mitigates the issue that once WebGL is loaded,

0 commit comments

Comments
 (0)