|
1 | 1 | import DroppableStrictMode from "../DroppableStrictMode"; |
2 | | -import { Config } from "../../types/Config"; |
3 | 2 |
|
4 | 3 | import styles from "./styles.module.css"; |
5 | 4 | import getClassNameFactory from "../../lib/get-class-name-factory"; |
6 | 5 | import { Draggable } from "../Draggable"; |
7 | 6 | import { DragIcon } from "../DragIcon"; |
| 7 | +import { ReactNode, useState } from "react"; |
| 8 | +import { useAppContext } from "../Puck/context"; |
| 9 | +import { ChevronDown, ChevronUp } from "react-feather"; |
8 | 10 |
|
9 | 11 | const getClassName = getClassNameFactory("ComponentList", styles); |
| 12 | +const getClassNameItem = getClassNameFactory("ComponentListItem", styles); |
10 | 13 |
|
11 | | -export const ComponentList = ({ config }: { config: Config }) => { |
| 14 | +const ComponentListItem = ({ |
| 15 | + component, |
| 16 | + index, |
| 17 | +}: { |
| 18 | + component: string; |
| 19 | + index: number; |
| 20 | +}) => { |
12 | 21 | return ( |
13 | | - <DroppableStrictMode droppableId="component-list" isDropDisabled> |
14 | | - {(provided, snapshot) => ( |
| 22 | + <div className={getClassNameItem()}> |
| 23 | + <Draggable |
| 24 | + key={component} |
| 25 | + id={component} |
| 26 | + index={index} |
| 27 | + showShadow |
| 28 | + disableAnimations |
| 29 | + className={() => getClassNameItem("draggable")} |
| 30 | + > |
| 31 | + {() => ( |
| 32 | + <> |
| 33 | + {component} |
| 34 | + <div className={getClassNameItem("icon")}> |
| 35 | + <DragIcon /> |
| 36 | + </div> |
| 37 | + </> |
| 38 | + )} |
| 39 | + </Draggable> |
| 40 | + </div> |
| 41 | + ); |
| 42 | +}; |
| 43 | + |
| 44 | +const ComponentList = ({ |
| 45 | + children, |
| 46 | + title, |
| 47 | + defaultExpanded = true, |
| 48 | +}: { |
| 49 | + children?: ReactNode; |
| 50 | + title?: string; |
| 51 | + defaultExpanded?: boolean; |
| 52 | +}) => { |
| 53 | + const { config } = useAppContext(); |
| 54 | + |
| 55 | + const [isExpanded, setIsExpanded] = useState(defaultExpanded); |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className={getClassName({ isExpanded })}> |
| 59 | + {title && ( |
15 | 60 | <div |
16 | | - {...provided.droppableProps} |
17 | | - ref={provided.innerRef} |
18 | | - className={getClassName({ |
19 | | - isDraggingFrom: !!snapshot.draggingFromThisWith, |
20 | | - })} |
| 61 | + className={getClassName("title")} |
| 62 | + onClick={() => setIsExpanded(!isExpanded)} |
| 63 | + title={ |
| 64 | + isExpanded |
| 65 | + ? `Collapse${title ? ` ${title}` : ""}` |
| 66 | + : `Expand${title ? ` ${title}` : ""}` |
| 67 | + } |
21 | 68 | > |
22 | | - {Object.keys(config.components).map((componentKey, i) => { |
23 | | - return ( |
24 | | - <Draggable |
25 | | - key={componentKey} |
26 | | - id={componentKey} |
27 | | - index={i} |
28 | | - showShadow |
29 | | - disableAnimations |
30 | | - className={() => getClassName("item")} |
31 | | - > |
32 | | - {() => ( |
33 | | - <> |
34 | | - {componentKey} |
35 | | - <div className={getClassName("itemIcon")}> |
36 | | - <DragIcon /> |
37 | | - </div> |
38 | | - </> |
39 | | - )} |
40 | | - </Draggable> |
41 | | - ); |
42 | | - })} |
43 | | - {/* Use different element so we don't clash with :last-of-type */} |
44 | | - <span style={{ display: "none" }}>{provided.placeholder}</span> |
| 69 | + <div>{title}</div> |
| 70 | + <div className={getClassName("titleIcon")}> |
| 71 | + {isExpanded ? <ChevronUp size={12} /> : <ChevronDown size={12} />} |
| 72 | + </div> |
45 | 73 | </div> |
46 | 74 | )} |
47 | | - </DroppableStrictMode> |
| 75 | + <div className={getClassName("content")}> |
| 76 | + <DroppableStrictMode |
| 77 | + droppableId={`component-list${title ? `:${title}` : ""}`} |
| 78 | + isDropDisabled |
| 79 | + > |
| 80 | + {(provided, snapshot) => ( |
| 81 | + <div |
| 82 | + {...provided.droppableProps} |
| 83 | + ref={provided.innerRef} |
| 84 | + className={getClassName({ |
| 85 | + isDraggingFrom: !!snapshot.draggingFromThisWith, |
| 86 | + })} |
| 87 | + > |
| 88 | + {children || |
| 89 | + Object.keys(config.components).map((componentKey, i) => { |
| 90 | + return ( |
| 91 | + <ComponentListItem |
| 92 | + key={componentKey} |
| 93 | + component={componentKey} |
| 94 | + index={i} |
| 95 | + /> |
| 96 | + ); |
| 97 | + })} |
| 98 | + {/* Use different element so we don't clash with :last-of-type */} |
| 99 | + <span style={{ display: "none" }}>{provided.placeholder}</span> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </DroppableStrictMode> |
| 103 | + </div> |
| 104 | + </div> |
48 | 105 | ); |
49 | 106 | }; |
| 107 | + |
| 108 | +ComponentList.Item = ComponentListItem; |
| 109 | + |
| 110 | +export { ComponentList }; |
0 commit comments