|
| 1 | +import React from "react"; |
| 2 | +import styled from "@emotion/styled"; |
| 3 | +import { |
| 4 | + ChevronRightIcon, |
| 5 | + ChevronDownIcon, |
| 6 | + DotsVerticalIcon, |
| 7 | +} from "@radix-ui/react-icons"; |
| 8 | +import { |
| 9 | + DropdownMenu, |
| 10 | + DropdownMenuTrigger, |
| 11 | + DropdownMenuContent, |
| 12 | + DropdownMenuItem, |
| 13 | +} from "@editor-ui/dropdown-menu"; |
| 14 | +import { IconButton } from "../components"; |
| 15 | +import Highlighter from "react-highlight-words"; |
| 16 | +import * as Collapsible from "@radix-ui/react-collapsible"; |
| 17 | + |
| 18 | +export type SectionHeaderAction = { |
| 19 | + label: string; |
| 20 | + handler: () => void; |
| 21 | +}; |
| 22 | + |
| 23 | +export function SectionHeader({ |
| 24 | + label, |
| 25 | + expanded = true, |
| 26 | + q, |
| 27 | + id, |
| 28 | + actions = [], |
| 29 | +}: { |
| 30 | + expanded?: boolean; |
| 31 | + label: string; |
| 32 | + q?: string; |
| 33 | + id: string; |
| 34 | + actions?: SectionHeaderAction[]; |
| 35 | +}) { |
| 36 | + const iconprops = { |
| 37 | + color: "white", |
| 38 | + }; |
| 39 | + |
| 40 | + const ToggleIcon = expanded ? ChevronDownIcon : ChevronRightIcon; |
| 41 | + |
| 42 | + return ( |
| 43 | + <SectionHeaderContainer id={id}> |
| 44 | + <Collapsible.Trigger asChild> |
| 45 | + <div className="leading"> |
| 46 | + <div className="toggle"> |
| 47 | + <ToggleIcon /> |
| 48 | + </div> |
| 49 | + <Highlighter |
| 50 | + className="label" |
| 51 | + highlightClassName="label" |
| 52 | + searchWords={q ? [q] : []} |
| 53 | + textToHighlight={label} |
| 54 | + autoEscape // required to escape regex special characters, like, `+`, `(`, `)`, etc. |
| 55 | + /> |
| 56 | + <div style={{ flex: 1 }} /> |
| 57 | + </div> |
| 58 | + </Collapsible.Trigger> |
| 59 | + <div className="actions"> |
| 60 | + {actions.length > 0 && ( |
| 61 | + <DropdownMenu> |
| 62 | + <DropdownMenuTrigger asChild> |
| 63 | + <IconButton> |
| 64 | + <DotsVerticalIcon /> |
| 65 | + </IconButton> |
| 66 | + </DropdownMenuTrigger> |
| 67 | + <DropdownMenuContent> |
| 68 | + {actions.map((action, i) => ( |
| 69 | + <DropdownMenuItem |
| 70 | + key={i} |
| 71 | + onSelect={(e) => { |
| 72 | + e.stopPropagation(); |
| 73 | + e.stopImmediatePropagation(); |
| 74 | + action.handler(); |
| 75 | + }} |
| 76 | + > |
| 77 | + {action.label} |
| 78 | + </DropdownMenuItem> |
| 79 | + ))} |
| 80 | + </DropdownMenuContent> |
| 81 | + </DropdownMenu> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + </SectionHeaderContainer> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +const SectionHeaderContainer = styled.div` |
| 89 | + display: flex; |
| 90 | + align-items: center; |
| 91 | + flex-direction: row; |
| 92 | + margin-bottom: 16px; |
| 93 | + border-radius: 4px; |
| 94 | + cursor: pointer; |
| 95 | +
|
| 96 | + background: transparent; |
| 97 | +
|
| 98 | + &:hover { |
| 99 | + background: rgba(255, 255, 255, 0.05); |
| 100 | + } |
| 101 | +
|
| 102 | + .leading { |
| 103 | + padding: 16px; |
| 104 | + display: flex; |
| 105 | + flex-direction: row; |
| 106 | + align-items: center; |
| 107 | + flex: 1; |
| 108 | + } |
| 109 | +
|
| 110 | + .toggle { |
| 111 | + margin-right: 16px; |
| 112 | + color: white; |
| 113 | + } |
| 114 | +
|
| 115 | + .actions { |
| 116 | + padding: 16px; |
| 117 | + margin-left: auto; |
| 118 | + } |
| 119 | +
|
| 120 | + .label { |
| 121 | + user-select: none; |
| 122 | + display: inline-block; |
| 123 | + opacity: 0.8; |
| 124 | + color: white; |
| 125 | + font-size: 18px; |
| 126 | + font-weight: 500; |
| 127 | + mark { |
| 128 | + background: white; |
| 129 | + color: black; |
| 130 | + } |
| 131 | + } |
| 132 | +`; |
0 commit comments