|
| 1 | +--- |
| 2 | +id: compound |
| 3 | +title: Compound Component |
| 4 | +sidebar_label: Compound Component |
| 5 | +--- |
| 6 | + |
| 7 | +Compound Component is a component that wraps at least one THREE.js entity in its template and all Inputs will be forwarded to that THREE.js entity. |
| 8 | + |
| 9 | +There are two steps to create a Compound component |
| 10 | + |
| 11 | +- Ensure that NGT Custom Renderer recognizes the Compound Component's selector as a `compound`. We can use `[compoundPrefixes]` Input on `<ngt-canvas>` to configure this |
| 12 | +- Add `ngtCompound` attribute on the THREE.js entity that we want our Compound Component to wrap |
| 13 | + |
| 14 | +Suppose we have the following template |
| 15 | + |
| 16 | +```html |
| 17 | +<ngt-mesh> |
| 18 | + <ngt-box-geometry *args="args" /> |
| 19 | + <ngt-mesh-basic-material /> |
| 20 | +</ngt-mesh> |
| 21 | + |
| 22 | +<ngt-mesh> |
| 23 | + <ngt-box-geometry *args="argsTwo" /> |
| 24 | + <ngt-mesh-standard-material /> |
| 25 | +</ngt-mesh> |
| 26 | +``` |
| 27 | + |
| 28 | +We notice that we keep using `<ngt-mesh>` and `<ngt-box-geometry>`. This is a good candidate to make into a Compound Component. In this case, |
| 29 | +we'll make a `Box` component. |
| 30 | + |
| 31 | +```ts |
| 32 | +extend({ Mesh, BoxGeometry }); |
| 33 | + |
| 34 | +@Component({ |
| 35 | + selector: 'tutorial-box', |
| 36 | + standalone: true, |
| 37 | + template: ` |
| 38 | + <ngt-mesh ngtCompound [ref]="boxRef"> |
| 39 | + <ngt-box-geometry *args="boxArgs" /> |
| 40 | + <ng-content /> |
| 41 | + </ngt-mesh> |
| 42 | + `, |
| 43 | + imports: [NgtArgs], |
| 44 | +}) |
| 45 | +export class Box { |
| 46 | + @Input() boxRef = injectNgtRef<Mesh>(); |
| 47 | + @Input() boxArgs: ConstructorParameters<typeof BoxGeometry> = [1, 1, 1]; |
| 48 | +} |
| 49 | + |
| 50 | +@Component({ |
| 51 | + template: `<ngt-canvas [compoundPrefixes]="['tutorial']" />`, |
| 52 | +}) |
| 53 | +export class SomeFeature {} |
| 54 | +``` |
| 55 | + |
| 56 | +- We use `<ngt-mesh>` and `<ngt-box-geometry>` with `<ng-content>`. This allows the consumers to pass in any Material, or other objects as children to our `<ngt-mesh>` |
| 57 | + |
| 58 | + - We use `ngtCompound` on the `<ngt-mesh>`. This lets NGT Custom Renderer knows that `Box` component will forward its Inputs to `<ngt-mesh>` |
| 59 | + :::note |
| 60 | + |
| 61 | + Inputs that are defined like `boxRef` and `boxArgs` will **not** get forwarded. |
| 62 | + |
| 63 | + ::: |
| 64 | + |
| 65 | +- We `extend({Mesh, BoxGeometry})` to ensure that NGT Custom Renderer knows about `Mesh` and `BoxGeometry` if they consume `Box` component |
| 66 | +- We let the NGT Custom Renderer knows that our `Box` component is a Compound Component by providing `['tutorial']` to `compoundPrefixes` |
| 67 | + |
| 68 | +Now that we have `Box`, we can consume it like following |
| 69 | + |
| 70 | +```html |
| 71 | +<tutorial-box> |
| 72 | + <ngt-mesh-basic-material /> |
| 73 | +</tutorial-box> |
| 74 | + |
| 75 | +<tutorial-box [position]="[2, 2, 2]"> |
| 76 | + <ngt-mesh-standard-material /> |
| 77 | +</tutorial-box> |
| 78 | + |
| 79 | +<tutorial-box [position]="[-2, -2, -2]"> |
| 80 | + <ngt-mesh-standard-material /> |
| 81 | + <ngt-mesh> |
| 82 | + <ngt-plane-geometry /> |
| 83 | + </ngt-mesh> |
| 84 | +</tutorial-box> |
| 85 | +``` |
| 86 | + |
| 87 | +Check out [TBD: angular-three-soba](#) for more examples on Compound Components. |
0 commit comments