@tamagui/core 1.92.0 → 1.93.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/core",
3
- "version": "1.92.0",
3
+ "version": "1.93.0",
4
4
  "source": "src/index.tsx",
5
5
  "main": "dist/cjs",
6
6
  "module": "dist/esm",
@@ -35,16 +35,16 @@
35
35
  "native-test.d.ts"
36
36
  ],
37
37
  "dependencies": {
38
- "@tamagui/react-native-use-pressable": "1.92.0",
39
- "@tamagui/react-native-use-responder-events": "1.92.0",
40
- "@tamagui/use-event": "1.92.0",
41
- "@tamagui/web": "1.92.0"
38
+ "@tamagui/react-native-use-pressable": "1.93.0",
39
+ "@tamagui/react-native-use-responder-events": "1.93.0",
40
+ "@tamagui/use-event": "1.93.0",
41
+ "@tamagui/web": "1.93.0"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "react": "*"
45
45
  },
46
46
  "devDependencies": {
47
- "@tamagui/build": "1.92.0",
47
+ "@tamagui/build": "1.93.0",
48
48
  "@testing-library/react": "^14.0.0",
49
49
  "csstype": "^3.0.10",
50
50
  "react": "^18.2.0",
package/reset.css CHANGED
@@ -4,6 +4,7 @@ body, #root, #__next {
4
4
  flex-direction: column;
5
5
  min-height: 100vh;
6
6
  min-width: calc(100vw - (100vw - 100%)); /* reserve space for scrollbar */
7
+ margin: 0;
7
8
  }
8
9
 
9
10
  * {
@@ -1,9 +1,9 @@
1
1
  import { useIsomorphicLayoutEffect } from '@tamagui/constants'
2
- import type { RefObject } from 'react'
2
+ import { type RefObject } from 'react'
3
3
  import { getBoundingClientRect } from '../helpers/getBoundingClientRect'
4
4
 
5
5
  const LayoutHandlers = new WeakMap<Element, Function>()
6
- const LayoutTimeouts = new WeakMap<Element, any>()
6
+ const resizeListeners = new Set<Function>()
7
7
 
8
8
  export type LayoutValue = {
9
9
  x: number
@@ -25,21 +25,41 @@ export type LayoutEvent = {
25
25
  let resizeObserver: ResizeObserver | null = null
26
26
 
27
27
  if (typeof window !== 'undefined' && 'ResizeObserver' in window) {
28
+ // node resize/move
28
29
  resizeObserver = new ResizeObserver((entries) => {
29
30
  for (const { target } of entries) {
30
31
  const onLayout = LayoutHandlers.get(target)
31
32
  if (typeof onLayout !== 'function') return
32
- measureLayout(target as HTMLElement, null, (x, y, width, height, left, top) => {
33
- onLayout({
34
- nativeEvent: {
35
- layout: { x, y, width, height, left, top },
36
- target,
37
- },
38
- timeStamp: Date.now(),
39
- })
33
+ measureElement(target as HTMLElement).then((event) => {
34
+ onLayout(event)
40
35
  })
41
36
  }
42
37
  })
38
+
39
+ // window resize
40
+ if (typeof window.addEventListener === 'function') {
41
+ let tm
42
+ window.addEventListener('resize', () => {
43
+ clearTimeout(tm)
44
+ tm = setTimeout(() => {
45
+ resizeListeners.forEach((cb) => cb())
46
+ }, 4)
47
+ })
48
+ }
49
+ }
50
+
51
+ export const measureElement = async (target: HTMLElement): Promise<LayoutEvent> => {
52
+ return new Promise((res) => {
53
+ measureLayout(target, null, (x, y, width, height, left, top) => {
54
+ res({
55
+ nativeEvent: {
56
+ layout: { x, y, width, height, left, top },
57
+ target,
58
+ },
59
+ timeStamp: Date.now(),
60
+ })
61
+ })
62
+ })
43
63
  }
44
64
 
45
65
  const cache = new WeakMap()
@@ -108,14 +128,27 @@ export function useElementLayout(
108
128
  ref: RefObject<Element>,
109
129
  onLayout?: ((e: LayoutEvent) => void) | null
110
130
  ) {
131
+ // two effects because expensive to re-run on every change of onLayout
111
132
  useIsomorphicLayoutEffect(() => {
112
- if (!resizeObserver || !onLayout) return
133
+ if (!onLayout) return
113
134
  const node = ref.current
114
135
  if (!node) return
115
136
  LayoutHandlers.set(node, onLayout)
137
+ }, [ref, onLayout])
138
+
139
+ useIsomorphicLayoutEffect(() => {
140
+ if (!resizeObserver) return
141
+ const node = ref.current
142
+ if (!node) return
143
+ if (!LayoutHandlers.has(node)) return
144
+ const onResize = () => {
145
+ measureElement(node as HTMLElement).then(onLayout)
146
+ }
147
+ resizeListeners.add(onResize)
116
148
  resizeObserver.observe(node)
117
149
  return () => {
150
+ resizeListeners.delete(onResize)
118
151
  resizeObserver?.unobserve(node)
119
152
  }
120
- }, [ref, onLayout])
153
+ }, [ref])
121
154
  }
@@ -1,4 +1,4 @@
1
- import type { RefObject } from 'react';
1
+ import { type RefObject } from 'react';
2
2
  export type LayoutValue = {
3
3
  x: number;
4
4
  y: number;
@@ -14,6 +14,7 @@ export type LayoutEvent = {
14
14
  };
15
15
  timeStamp: number;
16
16
  };
17
+ export declare const measureElement: (target: HTMLElement) => Promise<LayoutEvent>;
17
18
  export declare const measureLayout: (node: HTMLElement, relativeTo: HTMLElement | null, callback: (x: number, y: number, width: number, height: number, left: number, top: number) => void) => void;
18
19
  export declare function useElementLayout(ref: RefObject<Element>, onLayout?: ((e: LayoutEvent) => void) | null): void;
19
20
  //# sourceMappingURL=useElementLayout.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useElementLayout.d.ts","sourceRoot":"","sources":["../../src/hooks/useElementLayout.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAMtC,MAAM,MAAM,WAAW,GAAG;IACxB,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE;QACX,MAAM,EAAE,WAAW,CAAA;QACnB,MAAM,EAAE,GAAG,CAAA;KACZ,CAAA;IACD,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAwBD,eAAO,MAAM,aAAa,SAClB,WAAW,cACL,WAAW,GAAG,IAAI,gBAEzB,MAAM,KACN,MAAM,SACF,MAAM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