svelte-tv 1.0.1 → 1.0.3

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.
@@ -1,5 +1,6 @@
1
1
  import type { RouteContextValue } from './types.js';
2
2
  export declare function setRouterContext(value: RouteContextValue): void;
3
+ export declare function clearRouterContext(value: RouteContextValue): void;
3
4
  export declare function getRouterContext(): RouteContextValue;
4
5
  export declare function navigate(...args: Parameters<RouteContextValue['navigate']>): void;
5
6
  export declare function params(): import("./types.js").RouteParams;
@@ -129,6 +129,33 @@ const EFFECT_SHADER_KEYS = [
129
129
  'borderLeft',
130
130
  'shadow',
131
131
  ];
132
+ function getBoxValue(value, index, defaultValue = 0) {
133
+ if (value === undefined)
134
+ return defaultValue;
135
+ if (typeof value === 'number')
136
+ return value;
137
+ const len = value.length;
138
+ const result = len === 2
139
+ ? index % 2 === 0
140
+ ? value[0]
141
+ : value[1]
142
+ : len === 3
143
+ ? index === 0
144
+ ? value[0]
145
+ : index === 2
146
+ ? value[2]
147
+ : value[1]
148
+ : value[index];
149
+ return result ?? defaultValue;
150
+ }
151
+ function getPadding(node) {
152
+ return {
153
+ top: node.paddingTop ?? getBoxValue(node.padding, 0),
154
+ right: node.paddingRight ?? getBoxValue(node.padding, 1),
155
+ bottom: node.paddingBottom ?? getBoxValue(node.padding, 2),
156
+ left: node.paddingLeft ?? getBoxValue(node.padding, 3),
157
+ };
158
+ }
132
159
  const parseAndAssignShaderProps = (prefix, obj, props = {}) => {
133
160
  if (!obj)
134
161
  return;
@@ -1069,8 +1096,13 @@ export class ElementNode {
1069
1096
  const props = node.lng;
1070
1097
  const parentWidth = parent.w || 0;
1071
1098
  const parentHeight = parent.h || 0;
1072
- props.x = props.x || 0;
1073
- props.y = props.y || 0;
1099
+ const parentPadding = parent.display === 'flex' ? undefined : getPadding(parent);
1100
+ if (props.x === undefined) {
1101
+ props.x = parentPadding?.left ?? 0;
1102
+ }
1103
+ if (props.y === undefined) {
1104
+ props.y = parentPadding?.top ?? 0;
1105
+ }
1074
1106
  props.parent = parent.lng;
1075
1107
  if (this.right || this.right === 0) {
1076
1108
  props.x = parentWidth - this.right;
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">import { onDestroy, onMount } from "svelte";
2
2
  import RouteView from "./RouteView.svelte";
3
- import { setRouterContext } from "./context.js";
3
+ import { clearRouterContext, setRouterContext } from "./context.js";
4
4
  import { createLocation, matchRoutes, routeKey } from "./match.js";
5
5
  import { setRouteParentContext } from "./routeContext.js";
6
6
  let props = $props();
@@ -20,7 +20,7 @@ const navigate = (href, options) => {
20
20
  }
21
21
  window.location.hash = path;
22
22
  };
23
- setRouterContext({
23
+ const context = {
24
24
  registerRoute(route) {
25
25
  routes = [...routes, route];
26
26
  return () => {
@@ -32,7 +32,8 @@ setRouterContext({
32
32
  params: () => activeParams,
33
33
  routeData: () => activeData,
34
34
  currentRoute: () => activeRoute
35
- });
35
+ };
36
+ setRouterContext(context);
36
37
  setRouteParentContext({ registerRoute(route) {
37
38
  routes = [...routes, route];
38
39
  return () => {
@@ -75,7 +76,10 @@ onMount(() => {
75
76
  updateLocation();
76
77
  window.addEventListener("hashchange", updateLocation);
77
78
  });
78
- onDestroy(() => window.removeEventListener("hashchange", updateLocation));
79
+ onDestroy(() => {
80
+ window.removeEventListener("hashchange", updateLocation);
81
+ clearRouterContext(context);
82
+ });
79
83
  </script>
80
84
 
81
85
  {@render props.children?.()}
@@ -1,5 +1,6 @@
1
1
  import type { RouteContextValue } from './types.js';
2
2
  export declare function setRouterContext(value: RouteContextValue): void;
3
+ export declare function clearRouterContext(value: RouteContextValue): void;
3
4
  export declare function getRouterContext(): RouteContextValue;
4
5
  export declare function navigate(...args: Parameters<RouteContextValue['navigate']>): void;
5
6
  export declare function params(): import("./types.js").RouteParams;
@@ -1,11 +1,27 @@
1
1
  import { getContext, setContext } from 'svelte';
2
2
  const routerContext = Symbol('svelte-tv-router');
3
+ let activeRouterContext;
3
4
  export function setRouterContext(value) {
5
+ activeRouterContext = value;
4
6
  setContext(routerContext, value);
5
7
  }
8
+ export function clearRouterContext(value) {
9
+ if (activeRouterContext === value)
10
+ activeRouterContext = undefined;
11
+ }
6
12
  export function getRouterContext() {
7
- const context = getContext(routerContext);
13
+ let context;
14
+ try {
15
+ context = getContext(routerContext);
16
+ }
17
+ catch (error) {
18
+ if (activeRouterContext)
19
+ return activeRouterContext;
20
+ throw error;
21
+ }
8
22
  if (!context) {
23
+ if (activeRouterContext)
24
+ return activeRouterContext;
9
25
  throw new Error('Router context is not available.');
10
26
  }
11
27
  return context;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-tv",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Svelte speed for every screen 🚀✨",
5
5
  "type": "module",
6
6
  "exports": {
@@ -40,8 +40,11 @@
40
40
  "lint:fix": "prettier --write \"**/*.{svelte,ts,js,json,md}\"",
41
41
  "test": "vitest",
42
42
  "build:playground": "vite build playground --config ./vite.config.ts",
43
+ "build:playground:lab": "vite build playground-lab --config ./playground-lab/vite.config.ts",
43
44
  "preview": "vite preview playground --config ./vite.config.ts --host 0.0.0.0 --port 4173 --strictPort",
44
- "dev": "vite dev playground --config ./vite.config.ts --host 0.0.0.0 --port 5173 --strictPort"
45
+ "preview:lab": "vite preview playground-lab --config ./playground-lab/vite.config.ts --host 0.0.0.0 --port 4175 --strictPort",
46
+ "dev": "vite dev playground --config ./vite.config.ts --host 0.0.0.0 --port 5173",
47
+ "dev:lab": "vite dev playground-lab --config ./playground-lab/vite.config.ts --host 0.0.0.0 --port 5175 --strictPort"
45
48
  },
46
49
  "keywords": [
47
50
  "svelte",