Skip to content

Commit e9a7a72

Browse files
authored
fix(elements): accept <glyph-camera> alias as scene's parent camera (#4)
1 parent 5c9f39d commit e9a7a72

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

packages/glyphcss/src/elements/GlyphSceneElement.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect, beforeEach, afterEach } from "vitest";
22
import { GlyphSceneElement } from "./GlyphSceneElement";
33
import { GlyphPerspectiveCameraElement } from "./GlyphPerspectiveCameraElement";
4+
import { GlyphOrthographicCameraElement } from "./GlyphOrthographicCameraElement";
45

56
// Register elements if not already registered.
67
if (!customElements.get("glyph-scene")) {
@@ -9,6 +10,13 @@ if (!customElements.get("glyph-scene")) {
910
if (!customElements.get("glyph-perspective-camera")) {
1011
customElements.define("glyph-perspective-camera", GlyphPerspectiveCameraElement);
1112
}
13+
if (!customElements.get("glyph-orthographic-camera")) {
14+
customElements.define("glyph-orthographic-camera", GlyphOrthographicCameraElement);
15+
}
16+
if (!customElements.get("glyph-camera")) {
17+
class GlyphCameraElement extends GlyphOrthographicCameraElement {}
18+
customElements.define("glyph-camera", GlyphCameraElement);
19+
}
1220

1321
describe("GlyphSceneElement", () => {
1422
let camEl: GlyphPerspectiveCameraElement;
@@ -139,8 +147,17 @@ describe("GlyphSceneElement", () => {
139147
expect(() => {
140148
document.body.appendChild(orphanScene);
141149
}).toThrow(
142-
"glyphcss: <glyph-scene> must be placed inside a <glyph-perspective-camera> or <glyph-orthographic-camera>.",
150+
"glyphcss: <glyph-scene> must be placed inside a <glyph-camera>, <glyph-perspective-camera>, or <glyph-orthographic-camera>.",
143151
);
144152
orphanScene.remove();
145153
});
154+
155+
it("mounts inside the <glyph-camera> alias (orthographic alias)", () => {
156+
const aliasCam = document.createElement("glyph-camera");
157+
const aliasHost = document.createElement("glyph-scene") as GlyphSceneElement;
158+
aliasCam.appendChild(aliasHost);
159+
expect(() => { document.body.appendChild(aliasCam); }).not.toThrow();
160+
expect(aliasHost.getScene()).not.toBeNull();
161+
aliasCam.remove();
162+
});
146163
});

packages/glyphcss/src/elements/GlyphSceneElement.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ export class GlyphSceneElement extends ELEMENT_BASE {
9191
let el: HTMLElement | null = this.parentElement;
9292
while (el) {
9393
const tag = el.tagName.toLowerCase();
94-
if (tag === "glyph-perspective-camera" || tag === "glyph-orthographic-camera") {
94+
if (
95+
tag === "glyph-perspective-camera" ||
96+
tag === "glyph-orthographic-camera" ||
97+
tag === "glyph-camera"
98+
) {
9599
return el as HTMLElement & { getCamera?: () => unknown };
96100
}
97101
el = el.parentElement;
@@ -114,7 +118,7 @@ export class GlyphSceneElement extends ELEMENT_BASE {
114118
const cameraAncestor = this._findCameraAncestor();
115119
if (!cameraAncestor) {
116120
throw new Error(
117-
"glyphcss: <glyph-scene> must be placed inside a <glyph-perspective-camera> or <glyph-orthographic-camera>.",
121+
"glyphcss: <glyph-scene> must be placed inside a <glyph-camera>, <glyph-perspective-camera>, or <glyph-orthographic-camera>.",
118122
);
119123
}
120124
const cam = typeof cameraAncestor.getCamera === "function"

0 commit comments

Comments
 (0)