@hkdigital/lib-core 0.5.3 → 0.5.5

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.
@@ -38,6 +38,7 @@ export function enableContainerScaling({
38
38
  throw new Error('Container element is required for enableContainerScaling');
39
39
  }
40
40
 
41
+ /** @type {ResizeObserver} */
41
42
  let resizeObserver;
42
43
 
43
44
  /**
@@ -6,6 +6,8 @@
6
6
  getGameWidthOnPortrait
7
7
  } from './gamebox.util.js';
8
8
 
9
+ import ScaledContainer from './ScaledContainer.svelte';
10
+
9
11
  /**
10
12
  * @typedef {{
11
13
  * isLandscape: boolean,
@@ -191,9 +193,6 @@
191
193
  $inspect('windowWidth/Height', windowWidth, windowHeight);
192
194
  $inspect('iosWindowWidth/Height', iosWindowWidth, iosWindowHeight);
193
195
 
194
- // Game container reference
195
- let gameContainer = $state();
196
-
197
196
  // Update game dimensions based on window size and orientation
198
197
  $effect(() => {
199
198
  const width = iosWindowWidth ?? windowWidth;
@@ -246,33 +245,6 @@
246
245
  }
247
246
  });
248
247
 
249
- // Set up scaling if enabled, with orientation awareness
250
- $effect(() => {
251
- if (!enableScaling || !gameContainer || !gameWidth || !gameHeight) {
252
- return () => {}; // No-op cleanup if scaling not enabled or required elements missing
253
- }
254
-
255
- // Select the appropriate design based on orientation
256
- const activeDesign = isLandscape ? designLandscape : designPortrait;
257
-
258
- // console.debug(
259
- // `GameBox scaling [${isLandscape ? 'landscape' : 'portrait'}]:`,
260
- // `game: ${gameWidth}x${gameHeight}`,
261
- // `design: ${activeDesign.width}x${activeDesign.height}`
262
- // );
263
-
264
- // Apply scaling with the current design based on orientation
265
- return enableContainerScaling({
266
- container: gameContainer,
267
- design: activeDesign,
268
- clamping,
269
- getDimensions: () => ({
270
- width: gameWidth,
271
- height: gameHeight
272
- })
273
- });
274
- });
275
-
276
248
  let show = $state(false);
277
249
 
278
250
  let isPwa = $state(false);
@@ -458,7 +430,6 @@
458
430
  <div
459
431
  data-component="game-box"
460
432
  data-orientation={isLandscape ? 'landscape' : 'portrait'}
461
- bind:this={gameContainer}
462
433
  class="{base} {bg} {classes}"
463
434
  class:isMobile
464
435
  style:width="{gameWidth}px"
@@ -474,10 +445,13 @@
474
445
  <!-- Require fullscreen -->
475
446
  {#if isFullscreen && !isDevMode}
476
447
  <!-- Landscape content -->
477
- <div
478
- class:hidden={!isLandscape}
479
- style:width="{gameWidthOnLandscape}px"
480
- style:height="{gameHeightOnLandscape}px"
448
+ <ScaledContainer
449
+ enableScaling={enableScaling}
450
+ design={designLandscape}
451
+ {clamping}
452
+ width={gameWidthOnLandscape}
453
+ height={gameHeightOnLandscape}
454
+ hidden={!isLandscape}
481
455
  >
482
456
  {@render snippetLandscape({
483
457
  isLandscape,
@@ -493,12 +467,15 @@
493
467
  gameWidth,
494
468
  gameHeight
495
469
  })}
496
- </div>
470
+ </ScaledContainer>
497
471
  <!-- Portrait content -->
498
- <div
499
- class:hidden={isLandscape}
500
- style:width="{gameWidthOnPortrait}px"
501
- style:height="{gameHeightOnPortrait}px"
472
+ <ScaledContainer
473
+ enableScaling={enableScaling}
474
+ design={designPortrait}
475
+ {clamping}
476
+ width={gameWidthOnPortrait}
477
+ height={gameHeightOnPortrait}
478
+ hidden={isLandscape}
502
479
  >
503
480
  {@render snippetPortrait({
504
481
  isLandscape,
@@ -514,10 +491,16 @@
514
491
  gameWidth,
515
492
  gameHeight
516
493
  })}
517
- </div>
494
+ </ScaledContainer>
518
495
  {:else if supportsFullscreen && !isDevMode}
519
496
  <!-- Require fullscreen -->
520
- <div style:width="{gameWidth}px" style:height="{gameHeight}px">
497
+ <ScaledContainer
498
+ enableScaling={enableScaling}
499
+ design={isLandscape ? designLandscape : designPortrait}
500
+ {clamping}
501
+ width={gameWidth}
502
+ height={gameHeight}
503
+ >
521
504
  {@render snippetRequireFullscreen({
522
505
  isLandscape,
523
506
  isPortrait: !isLandscape,
@@ -532,10 +515,16 @@
532
515
  gameWidth,
533
516
  gameHeight
534
517
  })}
535
- </div>
518
+ </ScaledContainer>
536
519
  {:else if isMobile && snippetInstallOnHomeScreen && !isDevMode}
537
520
  <!-- Require install on home screen on mobile -->
538
- <div style:width="{gameWidth}px" style:height="{gameHeight}px">
521
+ <ScaledContainer
522
+ enableScaling={enableScaling}
523
+ design={isLandscape ? designLandscape : designPortrait}
524
+ {clamping}
525
+ width={gameWidth}
526
+ height={gameHeight}
527
+ >
539
528
  {@render snippetInstallOnHomeScreen({
540
529
  isLandscape,
541
530
  isPortrait: !isLandscape,
@@ -550,13 +539,16 @@
550
539
  gameWidth,
551
540
  gameHeight
552
541
  })}
553
- </div>
542
+ </ScaledContainer>
554
543
  {:else}
555
544
  <!-- Landscape content -->
556
- <div
557
- class:hidden={!isLandscape}
558
- style:width="{gameWidthOnLandscape}px"
559
- style:height="{gameHeightOnLandscape}px"
545
+ <ScaledContainer
546
+ enableScaling={enableScaling}
547
+ design={designLandscape}
548
+ {clamping}
549
+ width={gameWidthOnLandscape}
550
+ height={gameHeightOnLandscape}
551
+ hidden={!isLandscape}
560
552
  >
561
553
  {@render snippetLandscape({
562
554
  isLandscape,
@@ -572,12 +564,15 @@
572
564
  gameWidth,
573
565
  gameHeight
574
566
  })}
575
- </div>
567
+ </ScaledContainer>
576
568
  <!-- Portrait content -->
577
- <div
578
- class:hidden={isLandscape}
579
- style:width="{gameWidthOnPortrait}px"
580
- style:height="{gameHeightOnPortrait}px"
569
+ <ScaledContainer
570
+ enableScaling={enableScaling}
571
+ design={designPortrait}
572
+ {clamping}
573
+ width={gameWidthOnPortrait}
574
+ height={gameHeightOnPortrait}
575
+ hidden={isLandscape}
581
576
  >
582
577
  {@render snippetPortrait({
583
578
  isLandscape,
@@ -593,15 +588,18 @@
593
588
  gameWidth,
594
589
  gameHeight
595
590
  })}
596
- </div>
591
+ </ScaledContainer>
597
592
  {/if}
598
593
  {:else}
599
594
  <!-- Do not require fullscreen -->
600
595
  <!-- Landscape content -->
601
- <div
602
- class:hidden={!isLandscape}
603
- style:width="{gameWidthOnLandscape}px"
604
- style:height="{gameHeightOnLandscape}px"
596
+ <ScaledContainer
597
+ enableScaling={enableScaling}
598
+ design={designLandscape}
599
+ {clamping}
600
+ width={gameWidthOnLandscape}
601
+ height={gameHeightOnLandscape}
602
+ hidden={!isLandscape}
605
603
  >
606
604
  {@render snippetLandscape({
607
605
  isLandscape,
@@ -615,12 +613,15 @@
615
613
  gameWidth,
616
614
  gameHeight
617
615
  })}
618
- </div>
616
+ </ScaledContainer>
619
617
  <!-- Portrait content -->
620
- <div
621
- class:hidden={isLandscape}
622
- style:width="{gameWidthOnPortrait}px"
623
- style:height="{gameHeightOnPortrait}px"
618
+ <ScaledContainer
619
+ enableScaling={enableScaling}
620
+ design={designPortrait}
621
+ {clamping}
622
+ width={gameWidthOnPortrait}
623
+ height={gameHeightOnPortrait}
624
+ hidden={isLandscape}
624
625
  >
625
626
  {@render snippetPortrait({
626
627
  isLandscape,
@@ -634,7 +635,7 @@
634
635
  gameWidth,
635
636
  gameHeight
636
637
  })}
637
- </div>
638
+ </ScaledContainer>
638
639
  {/if}
639
640
  {/if}
640
641
  </div>
@@ -651,10 +652,6 @@
651
652
  /* border: solid 1px red;*/
652
653
  }
653
654
 
654
- .hidden {
655
- visibility: hidden;
656
- }
657
-
658
655
  :global(html.game-box-no-scroll) {
659
656
  overflow: clip;
660
657
  scrollbar-width: none; /* Firefox */
@@ -0,0 +1,88 @@
1
+ <script>
2
+ import { enableContainerScaling } from '../../../design/index.js';
3
+
4
+ /**
5
+ * @typedef {{
6
+ * enableScaling?: boolean,
7
+ * design?: {width: number, height: number},
8
+ * clamping?: {
9
+ * ui: {min: number, max: number},
10
+ * textBase: {min: number, max: number},
11
+ * textHeading: {min: number, max: number},
12
+ * textUi: {min: number, max: number}
13
+ * },
14
+ * width: number,
15
+ * height: number,
16
+ * hidden?: boolean,
17
+ * children: import('svelte').Snippet
18
+ * }}
19
+ */
20
+
21
+ /**
22
+ * Wrapper component that applies container scaling to its children
23
+ *
24
+ * @type {{
25
+ * enableScaling?: boolean,
26
+ * design?: {width: number, height: number},
27
+ * clamping?: {
28
+ * ui: {min: number, max: number},
29
+ * textBase: {min: number, max: number},
30
+ * textHeading: {min: number, max: number},
31
+ * textUi: {min: number, max: number}
32
+ * },
33
+ * width: number,
34
+ * height: number,
35
+ * hidden?: boolean,
36
+ * children: import('svelte').Snippet
37
+ * }}
38
+ */
39
+ let {
40
+ enableScaling = false,
41
+ design = undefined,
42
+ clamping = undefined,
43
+ width,
44
+ height,
45
+ hidden = false,
46
+ children
47
+ } = $props();
48
+
49
+ let container = $state();
50
+
51
+ // Apply container scaling when enabled and not hidden
52
+ $effect(() => {
53
+ if (
54
+ !enableScaling ||
55
+ !container ||
56
+ !width ||
57
+ !height ||
58
+ hidden ||
59
+ !design
60
+ ) {
61
+ return;
62
+ }
63
+
64
+ console.debug(`Enable scaling [${width},${height}]`);
65
+
66
+ return enableContainerScaling({
67
+ container,
68
+ design,
69
+ clamping,
70
+ getDimensions: () => ({ width, height })
71
+ });
72
+ });
73
+ </script>
74
+
75
+ <div
76
+ bind:this={container}
77
+ class:hidden
78
+ style:width="{width}px"
79
+ style:height="{height}px"
80
+ >
81
+ {@render children()}
82
+ </div>
83
+
84
+ <style>
85
+ .hidden {
86
+ visibility: hidden;
87
+ }
88
+ </style>
@@ -0,0 +1,62 @@
1
+ export default ScaledContainer;
2
+ type ScaledContainer = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<{
5
+ enableScaling?: boolean | undefined;
6
+ design?: {
7
+ width: number;
8
+ height: number;
9
+ } | undefined;
10
+ clamping?: {
11
+ ui: {
12
+ min: number;
13
+ max: number;
14
+ };
15
+ textBase: {
16
+ min: number;
17
+ max: number;
18
+ };
19
+ textHeading: {
20
+ min: number;
21
+ max: number;
22
+ };
23
+ textUi: {
24
+ min: number;
25
+ max: number;
26
+ };
27
+ } | undefined;
28
+ width: number;
29
+ height: number;
30
+ hidden?: boolean | undefined;
31
+ children: Snippet<[]>;
32
+ }>): void;
33
+ };
34
+ declare const ScaledContainer: import("svelte").Component<{
35
+ enableScaling?: boolean;
36
+ design?: {
37
+ width: number;
38
+ height: number;
39
+ };
40
+ clamping?: {
41
+ ui: {
42
+ min: number;
43
+ max: number;
44
+ };
45
+ textBase: {
46
+ min: number;
47
+ max: number;
48
+ };
49
+ textHeading: {
50
+ min: number;
51
+ max: number;
52
+ };
53
+ textUi: {
54
+ min: number;
55
+ max: number;
56
+ };
57
+ };
58
+ width: number;
59
+ height: number;
60
+ hidden?: boolean;
61
+ children: import("svelte").Snippet;
62
+ }, {}, "">;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"