Skip to content

Commit 659fd99

Browse files
committed
add code snippets to examples
1 parent b0ce5d5 commit 659fd99

File tree

6 files changed

+568
-4
lines changed

6 files changed

+568
-4
lines changed

examples/babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module.exports = function (api) {
22
api.cache(true);
33
return {
44
presets: ['babel-preset-expo'],
5+
plugins: ['react-native-reanimated/plugin'],
56
};
67
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
import React, { useState } from 'react';
3+
import {
4+
LayoutChangeEvent,
5+
Pressable,
6+
StyleSheet,
7+
Text,
8+
View,
9+
useColorScheme,
10+
} from 'react-native';
11+
import Animated, {
12+
useAnimatedStyle,
13+
useSharedValue,
14+
withTiming,
15+
} from 'react-native-reanimated';
16+
import SyntaxHighlighter from 'react-native-syntax-highlighter';
17+
import { ghcolors, tomorrow } from 'react-syntax-highlighter/dist/esm/styles/prism';
18+
19+
const styles = StyleSheet.create({
20+
container: {
21+
marginVertical: 10,
22+
borderRadius: 5,
23+
overflow: 'hidden',
24+
},
25+
header: {
26+
padding: 10,
27+
},
28+
headerText: {
29+
fontWeight: 'bold',
30+
},
31+
codeContainer: {
32+
position: 'absolute',
33+
top: 0,
34+
left: 0,
35+
right: 0,
36+
},
37+
});
38+
39+
interface CodeSnippetProps {
40+
code: string;
41+
}
42+
43+
export default function CodeSnippet({ code }: CodeSnippetProps): JSX.Element {
44+
const colorScheme = useColorScheme();
45+
const backgroundColor = colorScheme === 'dark' ? '#333' : '#f5f5f5';
46+
const color = colorScheme === 'dark' ? '#fff' : '#222';
47+
const syntaxStyle = colorScheme === 'dark' ? tomorrow : ghcolors;
48+
49+
const [isExpanded, setIsExpanded] = useState(false);
50+
const height = useSharedValue(0);
51+
const contentHeight = useSharedValue(0);
52+
53+
const animatedStyle = useAnimatedStyle(() => ({
54+
height: height.value,
55+
}));
56+
57+
const toggle = () => {
58+
height.value = withTiming(isExpanded ? 0 : contentHeight.value, {
59+
duration: 300,
60+
});
61+
setIsExpanded(!isExpanded);
62+
};
63+
64+
const onLayout = (event: LayoutChangeEvent) => {
65+
contentHeight.value = event.nativeEvent.layout.height;
66+
};
67+
68+
return (
69+
<View style={[styles.container, { backgroundColor }]}>
70+
<Pressable onPress={toggle} style={styles.header}>
71+
<Text style={[styles.headerText, { color }]}>
72+
{isExpanded ? 'Hide Code' : 'Show Code'}
73+
</Text>
74+
</Pressable>
75+
<Animated.View style={animatedStyle}>
76+
<View onLayout={onLayout} style={styles.codeContainer}>
77+
<SyntaxHighlighter language='jsx' style={syntaxStyle} highlighter={"prism"}>
78+
{code}
79+
</SyntaxHighlighter>
80+
</View>
81+
</Animated.View>
82+
</View>
83+
);
84+
}

examples/example-src-files/example.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import DropDownPicker, {
44
DropDownPickerProps,
55
ItemType,
66
} from 'react-native-dropdown-picker';
7+
import CodeSnippet from './CodeSnippet';
78

89
export interface ExampleProps {
910
multiple?: boolean;
1011
title: string;
1112
description?: string;
13+
code?: string;
1214
placeholder?: string;
1315
multipleText?: string;
1416
// For the sake of keeping the examples simple for now
@@ -131,6 +133,33 @@ export default function DropDownPickerExample({
131133
else setSingleValue(null);
132134
}}
133135
/>
136+
137+
<CodeSnippet
138+
code={`<DropDownPicker
139+
${dropdownProps ?
140+
` /* custom properties */ \n` + Object.entries(dropdownProps || {})
141+
.map(([key, value]) => {
142+
if (typeof value === 'string') {
143+
return ` ${key}='${value}'`;
144+
}
145+
return ` ${key}={${JSON.stringify(value)}}`;
146+
})
147+
.join('\n') + '\n\n' : ''} /* boilerplate */
148+
open={open}
149+
items={_items}
150+
setOpen={setOpen}
151+
setItems={setItems}
152+
theme={theme}
153+
placeholder={placeholder}
154+
${multiple ?
155+
` setValue={setMultiValue}
156+
value={multiValue}
157+
multiple` :
158+
` setValue={setSingleValue}
159+
value={singleValue}
160+
multiple={false}`}
161+
/>`}
162+
/>
134163
</View>
135164
</View>
136165
);

0 commit comments

Comments
 (0)