Skip to content

Commit 0a25000

Browse files
committed
feat: version 0.4.0
1 parent a6cd5f3 commit 0a25000

File tree

10 files changed

+118
-134
lines changed

10 files changed

+118
-134
lines changed

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ https://codesandbox.io/s/react-three-fiber-gui-62pvp
1313
Basic example
1414

1515
```tsx
16-
import { ControlsProvider, Controls, useControl } from 'react-three-gui';
16+
import { Controls, useControl } from 'react-three-gui';
1717

1818
export const App = () => {
1919
const rotationX = useControl('Rotation X', { type: 'number' });
2020
return (
21-
<ControlsProvider>
22-
<Canvas>
21+
<Controls.Provider>
22+
<Controls.Canvas>
2323
<mesh rotation-x={rotationX} />
24-
</Canvas>
24+
</Controls.Canvas>
2525
<Controls />
26-
</ControlsProvider>
26+
</Controls.Provider>
2727
);
2828
};
2929
```
@@ -73,6 +73,32 @@ useControl('Test', {
7373
});
7474
```
7575

76+
## Use your own Canvas
77+
78+
```tsx
79+
import { Canvas } from 'react-three-fiber';
80+
import { withControls } from 'react-three-gui';
81+
82+
// Wrap the <Canvas> with `withControls`
83+
const YourCanvas = withControls(Canvas);
84+
85+
const Scene = () => (
86+
<YourCanvas>
87+
<mesh rotation-x={rotationX} />
88+
</YourCanvas>
89+
);
90+
91+
const App = () => {
92+
const rotationX = useControl('Rotation X', { type: 'number' });
93+
return (
94+
<Controls.Provider>
95+
<Scene />
96+
<Controls />
97+
</Controls.Provider>
98+
);
99+
};
100+
```
101+
76102
## API
77103

78104
```tsx

example/index.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import 'react-app-polyfill/ie11';
33
import * as ReactDOM from 'react-dom';
44
import { animated } from 'react-spring';
55
import { a } from '@react-spring/three';
6-
import { Canvas, useLoader } from 'react-three-fiber';
6+
import { Canvas } from 'react-three-fiber';
77
import { Text } from 'drei';
88
import * as THREE from 'three';
9-
import { Controls, ControlsProvider, useControl, BaseControl } from '../src';
9+
import { Controls, useControl, withControls } from '../src';
1010
import { useEffect } from 'react';
1111

1212
const Next = () => {
@@ -18,7 +18,7 @@ const Next = () => {
1818
return (
1919
<a.mesh position={[1.5, 0, 0]} rotation-x={rotationX}>
2020
<boxGeometry attach="geometry" args={[1, 1, 1]} />
21-
<meshStandardMaterial attach="material" />
21+
<meshStandardMaterial attach="material" color="orange" />
2222
</a.mesh>
2323
);
2424
};
@@ -133,19 +133,39 @@ const Hello = () => {
133133
);
134134
};
135135

136+
const Sample2 = () => {
137+
const CtrlCanvas = withControls(Canvas);
138+
return (
139+
<CtrlCanvas style={{ width: 400, height: 400 }}>
140+
<ambientLight intensity={1} />
141+
<pointLight position={[0, 2, 2]} />
142+
<Next />
143+
</CtrlCanvas>
144+
);
145+
};
146+
147+
const Sample1 = () => {
148+
return (
149+
<Controls.Canvas style={{ width: 400, height: 400 }}>
150+
<ambientLight intensity={1} />
151+
<pointLight position={[0, 2, 2]} />
152+
<React.Suspense fallback={null}>
153+
<Box />
154+
</React.Suspense>
155+
</Controls.Canvas>
156+
);
157+
};
158+
136159
const App = () => {
137160
return (
138-
<ControlsProvider>
139-
<Canvas style={{ width: 800, height: 600 }}>
140-
<ambientLight intensity={1} />
141-
<pointLight position={[0, 2, 2]} />
142-
<React.Suspense fallback={null}>
143-
<Box />
144-
</React.Suspense>
145-
</Canvas>
161+
<Controls.Provider>
162+
<div style={{ display: 'flex', flexDirection: 'row' }}>
163+
<Sample1 />
164+
<Sample2 />
165+
</div>
146166
<Hello />
147167
<Controls />
148-
</ControlsProvider>
168+
</Controls.Provider>
149169
);
150170
};
151171

example/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"react-app-polyfill": "^1.0.6",
1414
"react-dom": ">= 16.8.0",
1515
"react-spring": "^8.0.27",
16-
"react-three-fiber": "^4.2.20",
17-
"three": "^0.120.0",
1816
"trim-right": "^1.0.1"
1917
},
2018
"alias": {

example/yarn.lock

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@
898898
"@babel/types" "^7.4.4"
899899
esutils "^2.0.2"
900900

901-
"@babel/runtime@^7.11.2", "@babel/runtime@^7.3.1", "@babel/runtime@^7.4.4", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
901+
"@babel/runtime@^7.11.2", "@babel/runtime@^7.3.1", "@babel/runtime@^7.4.4", "@babel/runtime@^7.8.4":
902902
version "7.11.2"
903903
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
904904
integrity sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==
@@ -976,11 +976,6 @@
976976
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.3.tgz#f060bf6eaafae4d56a7dac618980838b0696e2ab"
977977
integrity sha512-FmuxfCuolpLl0AnQ2NHSzoUKWEJDFl63qXjzdoWBVyFCXzMGm1spBzk7LeHNoVCiWCF7mRVms9e6jEV9+MoPbg==
978978

979-
"@juggle/resize-observer@^3.1.3":
980-
version "3.2.0"
981-
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.2.0.tgz#5e0b448d27fe3091bae6216456512c5904d05661"
982-
integrity sha512-fsLxt0CHx2HCV9EL8lDoVkwHffsA0snUpddYjdLyXcG5E41xaamn9ZyQqOE9TUJdrRlH8/hjIf+UdOdDeKCUgg==
983-
984979
"@mrmlnc/readdir-enhanced@^2.2.1":
985980
version "2.2.1"
986981
resolved "https://registry.yarnpkg.com/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde"
@@ -2091,11 +2086,6 @@ deasync@^0.1.14:
20912086
bindings "~1.2.1"
20922087
node-addon-api "^1.6.0"
20932088

2094-
debounce@^1.2.0:
2095-
version "1.2.0"
2096-
resolved "https://registry.yarnpkg.com/debounce/-/debounce-1.2.0.tgz#44a540abc0ea9943018dc0eaa95cce87f65cd131"
2097-
integrity sha512-mYtLl1xfZLi1m4RtQYlZgJUNQjl4ZxVnHzIR8nLLgi4q1YT8o/WM+MK/f8yfcc9s5Ir5zRaPZyZU6xs1Syoocg==
2098-
20992089
[email protected], debug@^2.2.0, debug@^2.3.3:
21002090
version "2.6.9"
21012091
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -4624,23 +4614,6 @@ react-merge-refs@^1.0.0:
46244614
resolved "https://registry.yarnpkg.com/react-merge-refs/-/react-merge-refs-1.1.0.tgz#73d88b892c6c68cbb7a66e0800faa374f4c38b06"
46254615
integrity sha512-alTKsjEL0dKH/ru1Iyn7vliS2QRcBp9zZPGoWxUOvRGWPUYgjo+V01is7p04It6KhgrzhJGnIj9GgX8W4bZoCQ==
46264616

4627-
react-promise-suspense@^0.3.2:
4628-
version "0.3.3"
4629-
resolved "https://registry.yarnpkg.com/react-promise-suspense/-/react-promise-suspense-0.3.3.tgz#b085c7e0ac22b85fd3d605b1c4f181cda4310bc9"
4630-
integrity sha512-OdehKsCEWYoV6pMcwxbvJH99UrbXylmXJ1QpEL9OfHaUBzcAihyfSJV8jFq325M/wW9iKc/BoiLROXxMul+MxA==
4631-
dependencies:
4632-
fast-deep-equal "^2.0.1"
4633-
4634-
4635-
version "0.25.1"
4636-
resolved "https://registry.yarnpkg.com/react-reconciler/-/react-reconciler-0.25.1.tgz#f9814d59d115e1210762287ce987801529363aaa"
4637-
integrity sha512-R5UwsIvRcSs3w8n9k3tBoTtUHdVhu9u84EG7E5M0Jk9F5i6DA1pQzPfUZd6opYWGy56MJOtV3VADzy6DRwYDjw==
4638-
dependencies:
4639-
loose-envify "^1.1.0"
4640-
object-assign "^4.1.1"
4641-
prop-types "^15.6.2"
4642-
scheduler "^0.19.1"
4643-
46444617
react-spring@^8.0.27:
46454618
version "8.0.27"
46464619
resolved "https://registry.yarnpkg.com/react-spring/-/react-spring-8.0.27.tgz#97d4dee677f41e0b2adcb696f3839680a3aa356a"
@@ -4649,29 +4622,6 @@ react-spring@^8.0.27:
46494622
"@babel/runtime" "^7.3.1"
46504623
prop-types "^15.5.8"
46514624

4652-
react-three-fiber@^4.2.20:
4653-
version "4.2.20"
4654-
resolved "https://registry.yarnpkg.com/react-three-fiber/-/react-three-fiber-4.2.20.tgz#2689c005689f7c706cfb29bcf7f09958ff900b9a"
4655-
integrity sha512-M5TCdGNKvofe37VMXnhbabgRDiRm9rnfxOmMFuLa+H9FiiLT3wUMMgE9eBmLh3McKgYXURETWKkNDePeQ0J0JQ==
4656-
dependencies:
4657-
"@babel/runtime" "^7.9.2"
4658-
"@juggle/resize-observer" "^3.1.3"
4659-
react-merge-refs "^1.0.0"
4660-
react-promise-suspense "^0.3.2"
4661-
react-reconciler "0.25.1"
4662-
react-use-measure "^2.0.0"
4663-
resize-observer-polyfill "^1.5.1"
4664-
scheduler "0.19.1"
4665-
tiny-emitter "^2.1.0"
4666-
utility-types "^3.10.0"
4667-
4668-
react-use-measure@^2.0.0:
4669-
version "2.0.1"
4670-
resolved "https://registry.yarnpkg.com/react-use-measure/-/react-use-measure-2.0.1.tgz#4f23f94c832cd4512da55acb300d1915dcbf3ae8"
4671-
integrity sha512-lFfHiqcXbJ2/6aUkZwt8g5YYM7EGqNVxJhMqMPqv1BVXRKp8D7jYLlmma0SvhRY4WYxxkZpCdbJvhDylb5gcEA==
4672-
dependencies:
4673-
debounce "^1.2.0"
4674-
46754625
"react@>= 16.8.0":
46764626
version "16.13.1"
46774627
resolved "https://registry.yarnpkg.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e"
@@ -4851,11 +4801,6 @@ request@^2.88.0:
48514801
tunnel-agent "^0.6.0"
48524802
uuid "^3.3.2"
48534803

4854-
resize-observer-polyfill@^1.5.1:
4855-
version "1.5.1"
4856-
resolved "https://registry.yarnpkg.com/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz#0e9020dd3d21024458d4ebd27e23e40269810464"
4857-
integrity sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==
4858-
48594804
resolve-from@^3.0.0:
48604805
version "3.0.0"
48614806
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-3.0.0.tgz#b22c7af7d9d6881bc8b6e653335eebcb0a188748"
@@ -4945,7 +4890,7 @@ saxes@^3.1.9:
49454890
dependencies:
49464891
xmlchars "^2.1.1"
49474892

4948-
scheduler@0.19.1, scheduler@^0.19.1:
4893+
scheduler@^0.19.1:
49494894
version "0.19.1"
49504895
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196"
49514896
integrity sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA==
@@ -5380,11 +5325,6 @@ terser@^4.1.2:
53805325
source-map "~0.6.1"
53815326
source-map-support "~0.5.12"
53825327

5383-
three@^0.120.0:
5384-
version "0.120.0"
5385-
resolved "https://registry.yarnpkg.com/three/-/three-0.120.0.tgz#238ab7a9e5d92af6bab3d257ea2b1891c90aef63"
5386-
integrity sha512-Swffpi3EAHWkmqC1MagKEzR5XgwkDiyeWI3M7vkGbBc0xhq2LcQmJj5DqBruLkrgcZQ+fM/+fSQBU1tDvggO4A==
5387-
53885328
through2@^2.0.0, through2@~2.0.3:
53895329
version "2.0.5"
53905330
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"
@@ -5405,11 +5345,6 @@ timsort@^0.3.0:
54055345
resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.3.0.tgz#405411a8e7e6339fe64db9a234de11dc31e02bd4"
54065346
integrity sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q=
54075347

5408-
tiny-emitter@^2.1.0:
5409-
version "2.1.0"
5410-
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423"
5411-
integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==
5412-
54135348
tiny-inflate@^1.0.0:
54145349
version "1.0.2"
54155350
resolved "https://registry.yarnpkg.com/tiny-inflate/-/tiny-inflate-1.0.2.tgz#93d9decffc8805bd57eae4310f0b745e9b6fb3a7"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-three-gui",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"main": "dist/index.js",
55
"module": "dist/react-three-gui.esm.js",
66
"typings": "dist/index.d.ts",

src/components/controls-provider.tsx

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
11
import React, { useState, useRef } from 'react';
22
import { ControlsContext } from '../contexts/controls-context';
3-
import { Canvas } from 'react-three-fiber';
3+
import { Canvas as R3FCanvas } from 'react-three-fiber';
44
import { ControlItem } from 'types';
5-
import { isMemo } from 'react-is';
6-
7-
const mapCanvasNode = (
8-
children: React.ReactElement,
9-
value: any
10-
): React.ReactElement[] => {
11-
return React.Children.map(children, item => {
12-
if (!item || !item.props) {
13-
return null;
14-
}
15-
16-
const isCanvas =
17-
item.type === Canvas ||
18-
(isMemo(item) && (item.type as any)?.type?.name === 'Canvas');
19-
20-
const childs = isCanvas ? (
21-
<ControlsContext.Provider value={value}>
22-
{item.props.children}
23-
</ControlsContext.Provider>
24-
) : (
25-
mapCanvasNode(item.props.children, value)
26-
);
27-
28-
return React.cloneElement(item, {
29-
...item.props,
30-
children: childs,
31-
});
32-
});
33-
};
345

356
export const ControlsProvider = ({
367
children,
@@ -63,11 +34,25 @@ export const ControlsProvider = ({
6334
},
6435
};
6536

66-
const childs = mapCanvasNode(children as React.ReactElement, context);
67-
6837
return (
6938
<ControlsContext.Provider value={context}>
70-
{childs}
39+
{children}
7140
</ControlsContext.Provider>
7241
);
7342
};
43+
44+
export function withControls(CanvasEl: typeof R3FCanvas) {
45+
return ({ children, ...props }: any) => (
46+
<ControlsContext.Consumer>
47+
{value => (
48+
<CanvasEl {...props}>
49+
<ControlsContext.Provider value={value}>
50+
{children}
51+
</ControlsContext.Provider>
52+
</CanvasEl>
53+
)}
54+
</ControlsContext.Consumer>
55+
);
56+
}
57+
58+
export const Canvas = withControls(R3FCanvas);

src/components/controls.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useContext } from 'react';
2+
import { ControlsProvider, Canvas } from './controls-provider';
23
import styled from 'styled-components';
34
import { animated, useSpring, to } from '@react-spring/web';
45
import { useDrag } from 'react-use-gesture';
@@ -159,7 +160,13 @@ const groupByGroup = (items: any): any => {
159160
}, {} as { [key: string]: any });
160161
};
161162

162-
export const Controls = (props: ControlsProps) => {
163+
interface ControlsFn {
164+
(props: ControlsProps): JSX.Element;
165+
Provider: typeof ControlsProvider;
166+
Canvas: typeof Canvas;
167+
}
168+
169+
export const Controls: ControlsFn = (props: ControlsProps) => {
163170
const {
164171
title = 'react-three-gui',
165172
defaultClosedGroups = [],
@@ -261,3 +268,6 @@ export const Controls = (props: ControlsProps) => {
261268
</Float>
262269
);
263270
};
271+
272+
Controls.Provider = ControlsProvider;
273+
Controls.Canvas = Canvas;

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { Controls } from './components/controls';
2-
export { ControlsProvider } from './components/controls-provider';
2+
export { withControls } from './components/controls-provider';
33
export { useControl } from './hooks/use-control';
44
export { BaseControl } from './components/controls/base-control';
5+
export { ControlsContext } from './contexts/controls-context';
56
export * from './types';

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ export const defaultValue = (options: ControlOptions) => {
3131
return undefined;
3232
};
3333

34-
export const clamp = (num: number, clamp: number, higher: number) =>
35-
higher ? Math.min(Math.max(num, clamp), higher) : Math.min(num, clamp);
34+
export const clamp = (num: number, clamp: number, higher?: number) =>
35+
higher !== undefined
36+
? Math.min(Math.max(num, clamp), higher)
37+
: Math.min(num, clamp);
3638

3739
export const map = (
3840
value: number,

0 commit comments

Comments
 (0)