|
| 1 | +import React from "react"; |
| 2 | +import { Platform, ScrollView, StyleSheet } from "react-native"; |
| 3 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 4 | + |
| 5 | +import { ThemedText } from "@/components/themed-text"; |
| 6 | +import { ThemedView } from "@/components/themed-view"; |
| 7 | +import { BottomTabInset, MaxContentWidth, Spacing } from "@/constants/theme"; |
| 8 | +import { useTheme } from "@/hooks/use-theme"; |
| 9 | + |
| 10 | +import { ReactNativeGrabContextProvider, ReactNativeGrabScreen } from "react-native-grab"; |
| 11 | + |
| 12 | +const formatContext = (value: Record<string, string | number | boolean | null>) => |
| 13 | + JSON.stringify(value); |
| 14 | + |
| 15 | +const ContextLabel = ({ value }: { value: Record<string, string | number | boolean | null> }) => { |
| 16 | + return ( |
| 17 | + <ThemedView type="backgroundSelected" style={styles.contextLabel}> |
| 18 | + <ThemedText type="code">adds: {formatContext(value)}</ThemedText> |
| 19 | + </ThemedView> |
| 20 | + ); |
| 21 | +}; |
| 22 | + |
| 23 | +export default function ContextPlaygroundScreen() { |
| 24 | + const safeAreaInsets = useSafeAreaInsets(); |
| 25 | + const theme = useTheme(); |
| 26 | + |
| 27 | + const rootContext = { area: "context-playground", release: "1.0", tracked: true } as const; |
| 28 | + const sectionContext = { section: "checkout-flow", density: "compact" } as const; |
| 29 | + const cardContext = { card: "payment-summary", currency: "USD", experiment: "B" } as const; |
| 30 | + const ctaContext = { action: "confirm-payment", priority: 1 } as const; |
| 31 | + |
| 32 | + const contentPlatformStyle = Platform.select({ |
| 33 | + default: { |
| 34 | + paddingTop: Spacing.three, |
| 35 | + paddingBottom: safeAreaInsets.bottom + BottomTabInset + Spacing.three, |
| 36 | + paddingLeft: safeAreaInsets.left, |
| 37 | + paddingRight: safeAreaInsets.right, |
| 38 | + }, |
| 39 | + android: { |
| 40 | + paddingTop: Spacing.three, |
| 41 | + paddingLeft: safeAreaInsets.left, |
| 42 | + paddingRight: safeAreaInsets.right, |
| 43 | + paddingBottom: safeAreaInsets.bottom + BottomTabInset + Spacing.three, |
| 44 | + }, |
| 45 | + web: { |
| 46 | + paddingTop: Spacing.six, |
| 47 | + paddingBottom: Spacing.four, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + return ( |
| 52 | + <ReactNativeGrabScreen> |
| 53 | + <ScrollView |
| 54 | + style={[styles.scrollView, { backgroundColor: theme.background }]} |
| 55 | + contentContainerStyle={[styles.contentContainer, contentPlatformStyle]} |
| 56 | + > |
| 57 | + <ThemedView style={styles.container}> |
| 58 | + <ThemedView style={styles.header}> |
| 59 | + <ThemedText type="subtitle">Grab Context Playground</ThemedText> |
| 60 | + <ThemedText themeColor="textSecondary"> |
| 61 | + Select any box below and verify the copied payload includes this hierarchy. |
| 62 | + </ThemedText> |
| 63 | + </ThemedView> |
| 64 | + |
| 65 | + <ReactNativeGrabContextProvider value={rootContext}> |
| 66 | + <ThemedView type="backgroundElement" style={styles.levelRoot}> |
| 67 | + <ThemedText type="smallBold">Level 1: App Area</ThemedText> |
| 68 | + <ContextLabel value={rootContext} /> |
| 69 | + |
| 70 | + <ReactNativeGrabContextProvider value={sectionContext}> |
| 71 | + <ThemedView type="backgroundElement" style={styles.levelSection}> |
| 72 | + <ThemedText type="smallBold">Level 2: Section</ThemedText> |
| 73 | + <ContextLabel value={sectionContext} /> |
| 74 | + |
| 75 | + <ReactNativeGrabContextProvider value={cardContext}> |
| 76 | + <ThemedView type="backgroundElement" style={styles.levelCard}> |
| 77 | + <ThemedText type="smallBold">Level 3: Card</ThemedText> |
| 78 | + <ContextLabel value={cardContext} /> |
| 79 | + |
| 80 | + <ReactNativeGrabContextProvider value={ctaContext}> |
| 81 | + <ThemedView type="backgroundSelected" style={styles.levelAction}> |
| 82 | + <ThemedText type="smallBold">Level 4: Action Button</ThemedText> |
| 83 | + <ContextLabel value={ctaContext} /> |
| 84 | + <ThemedText type="small" themeColor="textSecondary"> |
| 85 | + Grab this node to get all levels merged. |
| 86 | + </ThemedText> |
| 87 | + </ThemedView> |
| 88 | + </ReactNativeGrabContextProvider> |
| 89 | + </ThemedView> |
| 90 | + </ReactNativeGrabContextProvider> |
| 91 | + </ThemedView> |
| 92 | + </ReactNativeGrabContextProvider> |
| 93 | + </ThemedView> |
| 94 | + </ReactNativeGrabContextProvider> |
| 95 | + </ThemedView> |
| 96 | + </ScrollView> |
| 97 | + </ReactNativeGrabScreen> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +const styles = StyleSheet.create({ |
| 102 | + scrollView: { |
| 103 | + flex: 1, |
| 104 | + }, |
| 105 | + contentContainer: { |
| 106 | + flexDirection: "row", |
| 107 | + justifyContent: "center", |
| 108 | + }, |
| 109 | + container: { |
| 110 | + width: "100%", |
| 111 | + maxWidth: MaxContentWidth, |
| 112 | + paddingHorizontal: Spacing.four, |
| 113 | + gap: Spacing.four, |
| 114 | + }, |
| 115 | + header: { |
| 116 | + gap: Spacing.one, |
| 117 | + }, |
| 118 | + levelRoot: { |
| 119 | + borderRadius: Spacing.three, |
| 120 | + padding: Spacing.three, |
| 121 | + gap: Spacing.two, |
| 122 | + }, |
| 123 | + levelSection: { |
| 124 | + borderRadius: Spacing.three, |
| 125 | + padding: Spacing.three, |
| 126 | + gap: Spacing.two, |
| 127 | + marginTop: Spacing.one, |
| 128 | + }, |
| 129 | + levelCard: { |
| 130 | + borderRadius: Spacing.three, |
| 131 | + padding: Spacing.three, |
| 132 | + gap: Spacing.two, |
| 133 | + marginTop: Spacing.one, |
| 134 | + }, |
| 135 | + levelAction: { |
| 136 | + borderRadius: Spacing.three, |
| 137 | + padding: Spacing.three, |
| 138 | + gap: Spacing.one, |
| 139 | + marginTop: Spacing.one, |
| 140 | + }, |
| 141 | + contextLabel: { |
| 142 | + borderRadius: Spacing.two, |
| 143 | + paddingHorizontal: Spacing.two, |
| 144 | + paddingVertical: Spacing.one, |
| 145 | + }, |
| 146 | +}); |
0 commit comments