@inglorious/logo 3.0.26 → 3.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,7 +22,7 @@ Then, register the `logo` type in your store, create an entity of that type, and
22
22
 
23
23
  ```js
24
24
  import { createStore } from "@inglorious/store"
25
- import { logo } from "@inglorious/logo"
25
+ import { Logo } from "@inglorious/logo"
26
26
  import "@inglorious/logo/style.css"
27
27
 
28
28
  const entities = {
@@ -39,7 +39,7 @@ const entities = {
39
39
  }
40
40
 
41
41
  const store = createStore({
42
- types: { logo },
42
+ types: { Logo },
43
43
  entities,
44
44
  })
45
45
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/logo",
3
- "version": "3.0.26",
3
+ "version": "3.0.28",
4
4
  "description": "The Inglorious Coderz logo, remade to be compatible with Inglorious Web.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -44,15 +44,15 @@
44
44
  "**/*.css"
45
45
  ],
46
46
  "dependencies": {
47
- "@inglorious/web": "6.10.1"
47
+ "@inglorious/web": "^6.31.1"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@inglorious/eslint-config": "1.2.0",
51
- "prettier": "^3.9.6",
52
- "vitest": "^5.0.0"
51
+ "prettier": "^3.10.1",
52
+ "vitest": "^5.1.1"
53
53
  },
54
54
  "engines": {
55
- "node": ">= 22"
55
+ "node": ">= 26"
56
56
  },
57
57
  "types": "types/index.d.ts",
58
58
  "scripts": {
package/src/handlers.js CHANGED
@@ -1,95 +1,102 @@
1
1
  /**
2
- * @typedef {import("./template.js").LogoEntity} LogoEntity
3
- *
4
- * @typedef {Object} Api
5
- * @property {(id: string) => LogoEntity} getEntity
6
- * @property {(topic: string, payload: any) => void} notify
2
+ * @typedef {import("../types/index.d.ts").LogoEntity} LogoEntity
3
+ * @typedef {import("@inglorious/web").Api} Api
7
4
  */
8
- import { closestAncestor, isTouchDevice, saturate } from "./utils.js"
5
+ import { saturate } from "./utils.js"
9
6
 
10
7
  const MAX_HEAD_TILT_X = 400
11
8
  const MAX_HEAD_TILT_Y = 400
12
9
  const HALF = 0.5
13
- const FIRST_ITEM = 0
10
+ const FIRST_TOUCH = 0
14
11
 
15
- const eventType = isTouchDevice() ? "touchmove" : "mousemove"
16
- let moveListener = null
12
+ // Both, not one picked by sniffing the device: laptops with a touch screen
13
+ // have a mouse too.
14
+ const MOVE_EVENTS = ["mousemove", "touchmove"]
15
+ const listeners = new Map()
17
16
 
18
- export const Logo = {
17
+ export const handlers = {
19
18
  /**
20
- * Initialize interactivity for the given entity.
19
+ * Starts following the pointer when the entity is interactive.
21
20
  * @param {LogoEntity} entity
22
- * @param {string} id
21
+ * @param {unknown} payload
23
22
  * @param {Api} api
24
23
  */
25
24
  create(entity, payload, api) {
26
- if (!entity.isInteractive) {
27
- return
28
- }
25
+ if (!entity.isInteractive) return
29
26
 
30
27
  startInteraction(entity, api)
31
28
  },
32
29
 
33
30
  /**
34
- * Update the entity head tilt coordinates.
31
+ * Tilts the head towards the pointer.
35
32
  * @param {LogoEntity} entity
36
- * @param {{x:number,y:number}} payload
33
+ * @param {{ x: number, y: number }} payload - Pointer offset from the logo's center, in pixels.
37
34
  */
38
35
  coordsChange(entity, { x, y }) {
39
- entity.x = x
40
- entity.y = y
36
+ entity.position = [x, y]
41
37
  },
42
38
 
43
39
  /**
44
- * Cleanup listeners when the entity is destroyed.
45
40
  * @param {LogoEntity} entity
46
- * @param {string} id
47
41
  */
48
- destroy() {
49
- stopInteraction()
42
+ destroy(entity) {
43
+ stopInteraction(entity)
50
44
  },
51
45
  }
52
46
 
47
+ /**
48
+ * Makes the entity's logo follow the pointer. Each logo has its own listener,
49
+ * so several can follow it at once.
50
+ *
51
+ * @param {LogoEntity} entity
52
+ * @param {Api} api
53
+ */
53
54
  export function startInteraction(entity, api) {
54
- // skip during SSX
55
55
  if (typeof document === "undefined") return
56
56
 
57
- moveListener = createMoveListener(entity.id, api)
58
- document.addEventListener(eventType, moveListener, {
59
- passive: !entity.isScrollPrevented,
60
- })
57
+ stopInteraction(entity)
58
+ const listener = createMoveListener(entity.id, api)
59
+ listeners.set(entity.id, listener)
60
+ for (const eventType of MOVE_EVENTS) {
61
+ document.addEventListener(eventType, listener, {
62
+ passive: !entity.isScrollPrevented,
63
+ })
64
+ }
61
65
  }
62
66
 
63
- export function stopInteraction() {
64
- // skip during SSX
67
+ /**
68
+ * Stops the entity's logo from following the pointer, or every logo when
69
+ * called without one.
70
+ *
71
+ * @param {LogoEntity} [entity]
72
+ */
73
+ export function stopInteraction(entity) {
65
74
  if (typeof document === "undefined") return
66
75
 
67
- document.removeEventListener(eventType, moveListener)
76
+ const ids = entity ? [entity.id] : [...listeners.keys()]
77
+ for (const id of ids) {
78
+ for (const eventType of MOVE_EVENTS) {
79
+ document.removeEventListener(eventType, listeners.get(id))
80
+ }
81
+ listeners.delete(id)
82
+ }
68
83
  }
69
84
 
70
- /**
71
- * Create the move listener used by `create` / `fieldChange`.
72
- * The listener calculates the position of the pointer relative to
73
- * the element center and notifies the entity's `coordsChange`.
74
- *
75
- * @param {string} entityId
76
- * @param {Api} api
77
- * @returns {(event:MouseEvent|Touch) => void}
78
- */
79
85
  function createMoveListener(entityId, api) {
80
86
  return (event) => {
81
- const { left, top, width, height } = event.target.getBoundingClientRect()
87
+ const logo = findLogo(entityId)
88
+ if (!logo) return
82
89
 
90
+ const { left, top, width, height } = logo.getBoundingClientRect()
83
91
  const center = {
84
92
  x: window.scrollX + left + width * HALF,
85
93
  y: window.scrollY + top + height * HALF,
86
94
  }
87
95
 
88
- const { target, pageX, pageY } =
89
- eventType === "touchmove" ? event.touches[FIRST_ITEM] : event
96
+ const { pageX, pageY } = event.touches?.[FIRST_TOUCH] ?? event
90
97
 
91
98
  const entity = api.getEntity(entityId)
92
- if (entity.isScrollPrevented && closestAncestor(target, "logo")) {
99
+ if (entity.isScrollPrevented && logo.contains(event.target)) {
93
100
  event.preventDefault()
94
101
  }
95
102
 
@@ -99,3 +106,9 @@ function createMoveListener(entityId, api) {
99
106
  })
100
107
  }
101
108
  }
109
+
110
+ function findLogo(entityId) {
111
+ return [...document.querySelectorAll(".iw-logo")].find(
112
+ (element) => element.dataset.entityId === entityId,
113
+ )
114
+ }
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { Logo as handlers } from "./handlers.js"
2
- import * as renderers from "./template.js"
1
+ import { handlers } from "./handlers.js"
2
+ import { template } from "./template.js"
3
3
 
4
- export const Logo = { ...handlers, ...renderers }
4
+ export const Logo = { ...handlers, ...template }
5
5
 
6
6
  export { startInteraction, stopInteraction } from "./handlers.js"
package/src/template.js CHANGED
@@ -1,20 +1,8 @@
1
1
  /**
2
- * @typedef {Object} FaceSpec
3
- * @property {string} image - svg key (A..Z)
4
- * @property {boolean} [reverse] - whether the face image should be flipped horizontally
5
- * @property {boolean} [eye] - whether to render the small eye asset on the face
6
- *
7
- * @typedef {Object} LogoEntity
8
- * @property {string} id
9
- * @property {number} size
10
- * @property {number} x
11
- * @property {number} y
12
- * @property {[FaceSpec,FaceSpec]} faces - tuple: [leftFace, rightFace]
13
- * @property {boolean} [isInteractive]
14
- * @property {boolean} [isScrollPrevented]
2
+ * @typedef {import("../types/index.d.ts").LogoEntity} LogoEntity
15
3
  */
16
4
 
17
- import { html } from "@inglorious/web"
5
+ import { html, nothing } from "@inglorious/web"
18
6
  import { styleMap } from "@inglorious/web/directives/style-map"
19
7
  import { when } from "@inglorious/web/directives/when"
20
8
 
@@ -25,66 +13,70 @@ const DEFAULT_POSITION = [0, 0] // eslint-disable-line no-magic-numbers
25
13
  const DEFAULT_ROTATION = [-45, -40] // eslint-disable-line no-magic-numbers
26
14
  const DEFAULT_SIZE = 256
27
15
  const DEFAULT_SKEW = 12
28
- const STEP = 0.001
16
+ // 0.001 radians, the tilt the head has always had per pixel of pointer offset.
17
+ const TILT_DEGREES_PER_PIXEL = 0.0573
29
18
  const HALF = 0.5
30
19
 
31
- /**
32
- * Render the logo template for a `LogoEntity`.
33
- * The returned value is a lit-html `TemplateResult` produced by `html`.
34
- *
35
- * @param {LogoEntity} entity
36
- * @returns {import('lit-html').TemplateResult}
37
- */
38
- export function render(entity) {
39
- const {
40
- size = DEFAULT_SIZE,
41
- position = DEFAULT_POSITION,
42
- rotation = DEFAULT_ROTATION,
43
- skew = DEFAULT_SKEW,
44
- } = entity
45
- const [x, y] = position
46
- const [hRotation, vRotation] = rotation
47
- const [left, right] = entity.faces
20
+ export const template = {
21
+ /**
22
+ * Render the logo template for a `LogoEntity`.
23
+ * The returned value is a lit-html `TemplateResult` produced by `html`.
24
+ *
25
+ * @param {LogoEntity} entity
26
+ * @returns {import('lit-html').TemplateResult}
27
+ */
28
+ render(entity) {
29
+ const {
30
+ size = DEFAULT_SIZE,
31
+ position = DEFAULT_POSITION,
32
+ rotation = DEFAULT_ROTATION,
33
+ skew = DEFAULT_SKEW,
34
+ } = entity
35
+ const [x, y] = position
36
+ const [hRotation, vRotation] = rotation
37
+ const [left, right] = entity.faces
48
38
 
49
- return html`<div
50
- class="iw-logo"
51
- style=${styleMap({
52
- "--size": `${size}px`,
53
- "--transform": `scaleY(1.2) translateZ(-${size}px) rotateX(${
54
- vRotation - STEP * y
55
- }deg)
56
- rotateY(${hRotation + STEP * x}deg)`,
57
- "--z-translation": `${size * HALF}px`,
58
- "--left-face-flip": left.reverse ? "rotateY(180deg)" : "none",
59
- "--right-face-flip": right.reverse ? "rotateY(180deg)" : "none",
60
- "--skew": `${skew}deg`,
61
- })}
62
- >
63
- <div class="iw-logo-cube">
64
- <div class="iw-logo-cube-face left">
65
- <img src=${faces[left.image]} alt=${left.image} />
66
- ${when(
67
- left.eye,
68
- () =>
69
- html`<img
70
- class="iw-logo-cube-face-eye"
71
- src=${eye}
72
- alt="left eye"
73
- />`,
74
- )}
75
- </div>
76
- <div class="iw-logo-cube-face right">
77
- <img src=${faces[right.image]} alt=${right.image} />
78
- ${when(
79
- right.eye,
80
- () =>
81
- html`<img
82
- class="iw-logo-cube-face-eye"
83
- src=${eye}
84
- alt="right eye"
85
- />`,
86
- )}
39
+ return html`<div
40
+ class="iw-logo"
41
+ data-entity-id=${entity.id ?? nothing}
42
+ style=${styleMap({
43
+ "--size": `${size}px`,
44
+ "--transform": `scaleY(1.2) translateZ(-${size}px) rotateX(${
45
+ vRotation - TILT_DEGREES_PER_PIXEL * y
46
+ }deg)
47
+ rotateY(${hRotation + TILT_DEGREES_PER_PIXEL * x}deg)`,
48
+ "--z-translation": `${size * HALF}px`,
49
+ "--left-face-flip": left.reverse ? "rotateY(180deg)" : "none",
50
+ "--right-face-flip": right.reverse ? "rotateY(180deg)" : "none",
51
+ "--skew": `${skew}deg`,
52
+ })}
53
+ >
54
+ <div class="iw-logo-cube">
55
+ <div class="iw-logo-cube-face left">
56
+ <img src=${faces[left.image]} alt=${left.image} />
57
+ ${when(
58
+ left.eye,
59
+ () =>
60
+ html`<img
61
+ class="iw-logo-cube-face-eye"
62
+ src=${eye}
63
+ alt="left eye"
64
+ />`,
65
+ )}
66
+ </div>
67
+ <div class="iw-logo-cube-face right">
68
+ <img src=${faces[right.image]} alt=${right.image} />
69
+ ${when(
70
+ right.eye,
71
+ () =>
72
+ html`<img
73
+ class="iw-logo-cube-face-eye"
74
+ src=${eye}
75
+ alt="right eye"
76
+ />`,
77
+ )}
78
+ </div>
87
79
  </div>
88
- </div>
89
- </div>`
80
+ </div>`
81
+ },
90
82
  }
package/src/utils.js CHANGED
@@ -1,45 +1,5 @@
1
- export function isTouchDevice() {
2
- // skip during SSX
3
- if (typeof window === "undefined") return false
4
-
5
- if (
6
- "ontouchstart" in window ||
7
- (window.DocumentTouch && document instanceof window.DocumentTouch)
8
- ) {
9
- return true
10
- }
11
-
12
- // include the 'heartz' as a way to have a non matching mediaQuery to help terminate the join
13
- // https://git.io/vznFH
14
- const prefixes = " -webkit- -moz- -o- -ms- ".split(" ")
15
- var query = ["(", prefixes.join("touch-enabled),("), "heartz", ")"].join("")
16
- return window.matchMedia(query).matches
17
- }
18
-
19
1
  export function saturate(num, limit) {
20
2
  if (num < -limit) return -limit
21
3
  if (num > limit) return limit
22
4
  return num
23
5
  }
24
-
25
- export function closestAncestor(el, className) {
26
- const limit = 4
27
- let i = 0
28
- let closest = el
29
- while (closest && i < limit) {
30
- if (
31
- closest.className == null ||
32
- typeof closest.className.split !== "function"
33
- ) {
34
- return null
35
- }
36
-
37
- const classes = closest.className.split(" ")
38
- if (classes.includes(className)) {
39
- return closest
40
- }
41
-
42
- closest = closest.parentNode
43
- i++
44
- }
45
- }
package/types/index.d.ts CHANGED
@@ -39,9 +39,13 @@ export interface LogoEntity {
39
39
  id?: string
40
40
  type: string
41
41
  size: number
42
- x: number
43
- y: number
44
42
  faces: [Face, Face]
43
+ /** pointer offset from the center in pixels, set while interactive */
44
+ position?: [number, number]
45
+ /** horizontal and vertical rotation in degrees, default [-45, -40] */
46
+ rotation?: [number, number]
47
+ /** skew of the faces in degrees, default 12 */
48
+ skew?: number
45
49
  isInteractive?: boolean
46
50
  isScrollPrevented?: boolean
47
51
  }
@@ -53,4 +57,7 @@ export type LogoType = {
53
57
  destroy(entity: LogoEntity, payload: unknown, api: Api): void
54
58
  }
55
59
 
60
+ export declare function startInteraction(entity: LogoEntity, api: Api): void
61
+ export declare function stopInteraction(entity?: LogoEntity): void
62
+
56
63
  export declare const Logo: LogoType