Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flatlist for stories WIP #670

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-native-ui/src/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Explorer: FC<ExplorerProps> = React.memo(function Explorer({
const containerRef = useRef<View>(null);

return (
<View ref={containerRef} id="storybook-explorer-tree">
<View style={{ flex: 1 }} ref={containerRef} id="storybook-explorer-tree">
{dataset.entries.map(([refId, ref]) => (
<Ref
{...ref}
Expand Down
11 changes: 5 additions & 6 deletions packages/react-native-ui/src/MobileMenuDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
BottomSheetBackdrop,
BottomSheetBackdropProps,
BottomSheetModal,
BottomSheetScrollView,
BottomSheetView,
} from '@gorhom/bottom-sheet';
import { ReactNode, forwardRef, useImperativeHandle, useRef } from 'react';
import { Keyboard } from 'react-native';
Expand Down Expand Up @@ -65,14 +65,13 @@ export const MobileMenuDrawer = forwardRef<MobileMenuDrawerRef, MobileMenuDrawer
backgroundStyle={{ backgroundColor: theme.background.content }}
handleIndicatorStyle={{ backgroundColor: theme.textMutedColor }}
>
<BottomSheetScrollView
keyboardShouldPersistTaps="handled"
contentContainerStyle={{
paddingBottom: insets.bottom,
<BottomSheetView
style={{
flex: 1,
}}
>
{children}
</BottomSheetScrollView>
</BottomSheetView>
</BottomSheetModal>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-ui/src/Refs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface RefProps {

const Wrapper = styled.View<{ isMain: boolean }>(({}) => ({
position: 'relative',
flex: 1,
}));

export const Ref: FC<RefType & RefProps & { status?: State['status'] }> = React.memo(function Ref(
Expand Down
10 changes: 7 additions & 3 deletions packages/react-native-ui/src/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ const Swap = React.memo(function Swap({
const [a, b] = React.Children.toArray(children);
return (
<>
<View style={{ display: condition ? 'flex' : 'none' }}>{a}</View>
<View style={{ display: condition ? 'none' : 'flex' }}>{b}</View>
<View style={{ display: condition ? 'flex' : 'none', flex: condition ? 1 : undefined }}>
{a}
</View>
<View style={{ display: condition ? 'none' : 'flex', flex: condition ? undefined : 1 }}>
{b}
</View>
</>
);
});
Expand Down Expand Up @@ -97,7 +101,7 @@ export const Sidebar = React.memo(function Sidebar({
const lastViewedProps = useLastViewed(selected);

return (
<Container style={{ paddingHorizontal: 10 }}>
<Container style={{ flex: 1, paddingHorizontal: 10 }}>
<Top>
{/* <Heading
className="sidebar-header"
Expand Down
115 changes: 95 additions & 20 deletions packages/react-native-ui/src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type { ExpandAction, ExpandedState } from './hooks/useExpanded';
import { useExpanded } from './hooks/useExpanded';
import { getGroupStatus, statusMapping } from './util/status';
import { createId, getAncestorIds, getDescendantIds, isStoryHoistable } from './util/tree';
import { BottomSheetFlatList } from '@gorhom/bottom-sheet';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

interface NodeProps {
item: Item;
Expand Down Expand Up @@ -188,6 +190,8 @@ export const Tree = React.memo<{
selectedStoryId: string | null;
onSelectStoryId: (storyId: string) => void;
}>(function Tree({ isMain, refId, data, status, docsMode, selectedStoryId, onSelectStoryId }) {
const insets = useSafeAreaInsets();

const containerRef = useRef<View>(null);

// Find top-level nodes and group them so we can hoist any orphans and expand any roots.
Expand Down Expand Up @@ -290,8 +294,8 @@ export const Tree = React.memo<{

const groupStatus = useMemo(() => getGroupStatus(collapsedData, status), [collapsedData, status]);

const treeItems = useMemo(() => {
return collapsedItems.map((itemId) => {
const renderItem = useCallback(
({ item: itemId }) => {
const item = collapsedData[itemId];
const id = createId(itemId, refId);

Expand Down Expand Up @@ -337,32 +341,103 @@ export const Tree = React.memo<{
onSelectStoryId={onSelectStoryId}
/>
);
});
}, [
ancestry,
collapsedData,
collapsedItems,
docsMode,
expandableDescendants,
expanded,
groupStatus,
onSelectStoryId,
orphanIds,
refId,
selectedStoryId,
setExpanded,
status,
]);
},
[
ancestry,
collapsedData,
docsMode,
expandableDescendants,
expanded,
groupStatus,
onSelectStoryId,
orphanIds,
refId,
selectedStoryId,
setExpanded,
status,
]
);

// const treeItems = useMemo(() => {
// return collapsedItems.map((itemId) => {
// const item = collapsedData[itemId];
// const id = createId(itemId, refId);

// if (item.type === 'root') {
// const descendants = expandableDescendants[item.id];
// const isFullyExpanded = descendants.every((d: string) => expanded[d]);
// return (
// <Root
// key={id}
// item={item}
// refId={refId}
// isOrphan={false}
// isDisplayed
// isSelected={selectedStoryId === itemId}
// isExpanded={!!expanded[itemId]}
// setExpanded={setExpanded}
// isFullyExpanded={isFullyExpanded}
// expandableDescendants={descendants}
// onSelectStoryId={onSelectStoryId}
// docsMode={false}
// color=""
// status={{}}
// />
// );
// }

// const isDisplayed = !item.parent || ancestry[itemId].every((a: string) => expanded[a]);
// const color = groupStatus[itemId] ? statusMapping[groupStatus[itemId]][1] : null;

// return (
// <Node
// key={id}
// item={item}
// status={status?.[itemId]}
// refId={refId}
// color={color}
// docsMode={docsMode}
// isOrphan={orphanIds.some((oid) => itemId === oid || itemId.startsWith(`${oid}-`))}
// isDisplayed={isDisplayed}
// isSelected={selectedStoryId === itemId}
// isExpanded={!!expanded[itemId]}
// setExpanded={setExpanded}
// onSelectStoryId={onSelectStoryId}
// />
// );
// });
// }, [
// ancestry,
// collapsedData,
// collapsedItems,
// docsMode,
// expandableDescendants,
// expanded,
// groupStatus,
// onSelectStoryId,
// orphanIds,
// refId,
// selectedStoryId,
// setExpanded,
// status,
// ]);
return (
<Container ref={containerRef} hasOrphans={isMain && orphanIds.length > 0}>
{treeItems}
<BottomSheetFlatList
contentContainerStyle={{ paddingTop: 20, paddingBottom: insets.bottom + 16 }}
style={{ flex: 1 }}
keyboardShouldPersistTaps="handled"
data={collapsedItems}
renderItem={renderItem}
/>
</Container>
);
});

const Container = styled.View<{ hasOrphans: boolean }>((props) => ({
marginTop: props.hasOrphans ? 20 : 0,
// marginTop: props.hasOrphans ? 20 : 0,
marginBottom: 20,
flex: 1,
}));

const Root = React.memo<NodeProps & { expandableDescendants: string[] }>(function Root({
Expand Down
Loading