@farcaster/snap 2.6.2 → 2.6.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 { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Image } from "expo-image";
|
|
3
|
+
import { useCallback, useState } from "react";
|
|
3
4
|
import { StyleSheet, Text, View } from "react-native";
|
|
4
5
|
import { useSnapStackDirection } from "../stack-direction-context.js";
|
|
5
6
|
function aspectToRatio(aspect) {
|
|
@@ -17,10 +18,16 @@ export function SnapImage({ element: { props }, }) {
|
|
|
17
18
|
const ratio = aspectToRatio(String(props.aspect ?? "1:1"));
|
|
18
19
|
const stackDir = useSnapStackDirection();
|
|
19
20
|
const inHorizontalStack = stackDir === "horizontal";
|
|
20
|
-
|
|
21
|
+
const [frameWidth, setFrameWidth] = useState(0);
|
|
22
|
+
const measuredHeight = frameWidth > 0 ? frameWidth / ratio : undefined;
|
|
23
|
+
const handleLayout = useCallback((event) => {
|
|
24
|
+
const nextWidth = Math.round(event.nativeEvent.layout.width);
|
|
25
|
+
setFrameWidth((currentWidth) => currentWidth !== nextWidth ? nextWidth : currentWidth);
|
|
26
|
+
}, []);
|
|
27
|
+
return (_jsxs(View, { onLayout: handleLayout, style: [
|
|
21
28
|
styles.frame,
|
|
22
29
|
inHorizontalStack ? styles.frameInHorizontalRow : styles.frameFullWidth,
|
|
23
|
-
{ aspectRatio: ratio },
|
|
30
|
+
measuredHeight ? { height: measuredHeight } : { aspectRatio: ratio },
|
|
24
31
|
], children: [_jsx(Image, { source: { uri: url }, style: StyleSheet.absoluteFill, contentFit: "cover", accessibilityLabel: alt || undefined }), hasOverlay ? (_jsx(View, { style: styles.overlay, pointerEvents: "none", children: _jsxs(View, { style: styles.overlayContent, children: [title ? (_jsx(Text, { numberOfLines: 1, style: styles.title, children: title })) : null, subtitle ? (_jsx(Text, { numberOfLines: 1, style: styles.subtitle, children: subtitle })) : null] }) })) : null] }));
|
|
25
32
|
}
|
|
26
33
|
const styles = StyleSheet.create({
|
package/package.json
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { ComponentRenderProps } from "@json-render/react-native";
|
|
2
2
|
import { Image } from "expo-image";
|
|
3
|
+
import { useCallback, useState } from "react";
|
|
4
|
+
import type { ViewProps } from "react-native";
|
|
3
5
|
import { StyleSheet, Text, View } from "react-native";
|
|
4
6
|
import { useSnapStackDirection } from "../stack-direction-context";
|
|
5
7
|
|
|
@@ -20,13 +22,22 @@ export function SnapImage({
|
|
|
20
22
|
const ratio = aspectToRatio(String(props.aspect ?? "1:1"));
|
|
21
23
|
const stackDir = useSnapStackDirection();
|
|
22
24
|
const inHorizontalStack = stackDir === "horizontal";
|
|
25
|
+
const [frameWidth, setFrameWidth] = useState(0);
|
|
26
|
+
const measuredHeight = frameWidth > 0 ? frameWidth / ratio : undefined;
|
|
27
|
+
const handleLayout = useCallback<NonNullable<ViewProps["onLayout"]>>((event) => {
|
|
28
|
+
const nextWidth = Math.round(event.nativeEvent.layout.width);
|
|
29
|
+
setFrameWidth((currentWidth) =>
|
|
30
|
+
currentWidth !== nextWidth ? nextWidth : currentWidth,
|
|
31
|
+
);
|
|
32
|
+
}, []);
|
|
23
33
|
|
|
24
34
|
return (
|
|
25
35
|
<View
|
|
36
|
+
onLayout={handleLayout}
|
|
26
37
|
style={[
|
|
27
38
|
styles.frame,
|
|
28
39
|
inHorizontalStack ? styles.frameInHorizontalRow : styles.frameFullWidth,
|
|
29
|
-
{ aspectRatio: ratio },
|
|
40
|
+
measuredHeight ? { height: measuredHeight } : { aspectRatio: ratio },
|
|
30
41
|
]}
|
|
31
42
|
>
|
|
32
43
|
<Image
|