Skip to content

Commit

Permalink
chore: add example for drawer-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Mar 12, 2024
1 parent 03adf8c commit 7afd99a
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@react-navigation/material-bottom-tabs",
"@react-navigation/elements",
"@react-navigation/devtools",
"react-native-tab-view"
"react-native-tab-view",
"react-native-drawer-layout"
]
},
"env": {
Expand Down
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"check:prebuild": "node ../scripts/check-prebuild.js"
},
"dependencies": {
"@expo/react-native-action-sheet": "^4.0.1",
"@expo/vector-icons": "^13.0.0",
"@react-native-async-storage/async-storage": "1.17.11",
"@react-native-masked-view/masked-view": "0.2.9",
Expand Down
160 changes: 160 additions & 0 deletions example/src/Screens/DrawerView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { useActionSheet } from '@expo/react-native-action-sheet';
import { useTheme } from '@react-navigation/native';
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import { Drawer, useDrawerProgress } from 'react-native-drawer-layout';
import { Button, Text } from 'react-native-paper';
import Animated, {
interpolate,
// @ts-expect-error the type definitions are incomplete
isConfigured,
type SharedValue,
useAnimatedStyle,
} from 'react-native-reanimated';

const RealDrawer = () => {
const progress = useDrawerProgress() as SharedValue<number>;

const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateY: interpolate(progress.value, [0, 1], [56, 0]),
},
],
};
});

return (
<View style={styles.realDrawerWrapper}>
<Animated.View style={[styles.realDrawer, animatedStyle]} />
</View>
);
};

const DRAWER_TYPES = ['front', 'back', 'slide'] as const;

export default function DrawerView() {
const { showActionSheetWithOptions } = useActionSheet();
const { colors } = useTheme();

const [open, setOpen] = React.useState(false);
const [legacy, setLegacy] = React.useState(isConfigured?.() ? false : true);
const [drawerType, setDrawerType] =
React.useState<(typeof DRAWER_TYPES)[number]>('front');
const [drawerPosition, setDrawerPosition] = React.useState<'left' | 'right'>(
'left'
);

return (
<Drawer
useLegacyImplementation={legacy}
open={open}
onOpen={() => setOpen(true)}
onClose={() => setOpen(false)}
drawerType={drawerType}
drawerPosition={drawerPosition}
renderDrawerContent={() => {
return (
<View style={styles.container}>
<Button color="tomato" onPress={() => setOpen(false)}>
Close drawer
</Button>
</View>
);
}}
>
<View style={[styles.container, { backgroundColor: colors.background }]}>
{!legacy && <RealDrawer />}
<View style={styles.buttons}>
<Button
mode="contained"
onPress={() => setOpen((prevOpen) => !prevOpen)}
>
{open ? 'Close' : 'Open'} drawer
</Button>
<Button
mode="outlined"
onPress={() =>
setDrawerPosition((prevPosition) =>
prevPosition === 'left' ? 'right' : 'left'
)
}
>
Change position ({drawerPosition})
</Button>
<Button
mode="outlined"
onPress={() =>
showActionSheetWithOptions(
{
options: DRAWER_TYPES.map((option) => {
if (option === drawerType) {
return `${option} (current)`;
}

return option;
}),
},
(index) => {
if (index != null) {
setDrawerType(DRAWER_TYPES[index]);
}
}
)
}
>
Change type ({drawerType})
</Button>
{isConfigured?.() ? (
<Button
mode="outlined"
onPress={() => setLegacy((prevLegacy) => !prevLegacy)}
>
Change Reanimated ({legacy ? 1 : 2})
</Button>
) : (
<Text>Using Reanimated 1</Text>
)}
</View>
</View>
</Drawer>
);
}

DrawerView.title = 'Drawer Layout';
DrawerView.linking = {};
DrawerView.options = {
headerShown: true,
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 8,
},
buttons: {
gap: 8,
alignItems: 'center',
},
realDrawerWrapper: {
width: 64,
height: 72,
marginBottom: 8,
overflow: 'hidden',
},
realDrawer: {
position: 'absolute',
top: 0,
start: 0,
end: 8,
bottom: 0,
backgroundColor: '#ebdec1',
borderColor: '#3e3a3a',
borderWidth: 4,
borderBottomWidth: 0,
borderRadius: 2,
},
});
21 changes: 19 additions & 2 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ActionSheetProvider } from '@expo/react-native-action-sheet';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {
useFlipper,
Expand Down Expand Up @@ -128,7 +129,7 @@ export default function App() {
const isLargeScreen = dimensions.width >= 1024;

return (
<PaperProvider theme={paperTheme}>
<Providers theme={paperTheme}>
<StatusBar
translucent
barStyle={theme.dark ? 'light-content' : 'dark-content'}
Expand Down Expand Up @@ -299,6 +300,22 @@ export default function App() {
))}
</Stack.Navigator>
</NavigationContainer>
</PaperProvider>
</Providers>
);
}

const Providers = ({
theme,
children,
}: {
theme: ReactNativePaper.Theme;
children: React.ReactNode;
}) => {
return (
<PaperProvider theme={theme}>
<ActionSheetProvider>
<>{children}</>
</ActionSheetProvider>
</PaperProvider>
);
};
5 changes: 5 additions & 0 deletions example/src/screens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NavigatorScreenParams } from '@react-navigation/native';

import AuthFlow from './Screens/AuthFlow';
import BottomTabs from './Screens/BottomTabs';
import DrawerView from './Screens/DrawerView';
import DynamicTabs from './Screens/DynamicTabs';
import LinkComponent from './Screens/LinkComponent';
import MasterDetail from './Screens/MasterDetail';
Expand Down Expand Up @@ -101,6 +102,10 @@ export const SCREENS = {
title: 'TabView',
component: TabView,
},
DrawerLayout: {
title: 'Drawer Layout',
component: DrawerView,
},
};

type ParamListTypes = {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"paths": {
"@react-navigation/*": ["./packages/*/src"],
"react-native-tab-view": ["./packages/react-native-tab-view/src"],
"react-native-drawer-layout": ["./packages/react-native-drawer-layout/src"],
},
"composite": true,
"allowUnreachableCode": false,
Expand Down
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3930,6 +3930,18 @@ __metadata:
languageName: node
linkType: hard

"@expo/react-native-action-sheet@npm:^4.0.1":
version: 4.0.1
resolution: "@expo/react-native-action-sheet@npm:4.0.1"
dependencies:
"@types/hoist-non-react-statics": ^3.3.1
hoist-non-react-statics: ^3.3.0
peerDependencies:
react: ">=16.3.0"
checksum: f33b8a3b7c6ca77a6e05e5b2284c0961cf1e27b954e87b76747afc990a9581b852326e1ab739ac84a2c1afce05a133627c85465418cad5292d162fbcb305d86e
languageName: node
linkType: hard

"@expo/rudder-sdk-node@npm:1.1.1":
version: 1.1.1
resolution: "@expo/rudder-sdk-node@npm:1.1.1"
Expand Down Expand Up @@ -5658,6 +5670,7 @@ __metadata:
dependencies:
"@babel/core": ^7.20.5
"@babel/node": ^7.20.5
"@expo/react-native-action-sheet": ^4.0.1
"@expo/vector-icons": ^13.0.0
"@expo/webpack-config": ^18.0.1
"@playwright/test": ^1.28.1
Expand Down Expand Up @@ -6283,6 +6296,16 @@ __metadata:
languageName: node
linkType: hard

"@types/hoist-non-react-statics@npm:^3.3.1":
version: 3.3.5
resolution: "@types/hoist-non-react-statics@npm:3.3.5"
dependencies:
"@types/react": "*"
hoist-non-react-statics: ^3.3.0
checksum: b645b062a20cce6ab1245ada8274051d8e2e0b2ee5c6bd58215281d0ec6dae2f26631af4e2e7c8abe238cdcee73fcaededc429eef569e70908f82d0cc0ea31d7
languageName: node
linkType: hard

"@types/html-minifier-terser@npm:^6.0.0":
version: 6.1.0
resolution: "@types/html-minifier-terser@npm:6.1.0"
Expand Down

0 comments on commit 7afd99a

Please sign in to comment.