-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathExample.tsx
More file actions
84 lines (78 loc) · 2.44 KB
/
Copy pathExample.tsx
File metadata and controls
84 lines (78 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {useState, useEffect} from 'react';
import * as React from 'react';
import {Code} from './Code';
import {StyleSheet, PlatformColor, Text, View, Dimensions} from 'react-native';
import {CopyToClipboardButton} from './CopyToClipboard';
import {useTheme} from '../Navigation';
const createStyles = (colors: any, windowWidth: number) => {
const isSmallScreen = windowWidth < 600;
const padding = isSmallScreen ? 8 : 15;
return StyleSheet.create({
title: {
marginTop: isSmallScreen ? 20 : 30,
marginBottom: 10,
fontSize: isSmallScreen ? 18 : 20,
color: PlatformColor('TextControlForeground'),
},
box: {
borderRadius: 8,
borderWidth: 1,
borderColor: colors.border,
},
exampleContainer: {
padding: padding,
backgroundColor: PlatformColor('Background'),
minHeight: isSmallScreen ? 40 : 50,
justifyContent: 'center',
alignItems: 'center',
},
codeContainer: {
borderWidth: 0,
borderTopWidth: 1,
borderColor: colors.border,
padding: isSmallScreen ? 8 : 12,
backgroundColor: PlatformColor('Background'),
},
});
};
export const Example = React.forwardRef<any, {
title: string;
code: string;
children: React.ReactNode;
}>((props, ref) => {
const {colors} = useTheme();
const [windowDimensions, setWindowDimensions] = useState(Dimensions.get('window'));
useEffect(() => {
const subscription = Dimensions.addEventListener('change', ({window}) => {
setWindowDimensions(window);
});
return () => subscription?.remove();
}, []);
const styles = createStyles(colors, windowDimensions.width);
return (
<View>
<Text accessibilityRole={'header'} accessibilityLevel={3} style={styles.title}>
{props.title}
</Text>
{props.code ? (
<View style={styles.box}>
<View style={styles.exampleContainer}>{props.children}</View>
<View
style={styles.codeContainer}
accessible={true}
accessibilityRole="none"
accessibilityLabel="Source code">
<Code>{props.code}</Code>
<View style={{position: 'absolute', right: 12, top: 12}}>
<CopyToClipboardButton ref={ref} content={props.code} />
</View>
</View>
</View>
) : (
<View style={styles.box}>
<View style={styles.exampleContainer}>{props.children}</View>
</View>
)}
</View>
);
});