|
1 | 1 | 'use client';
|
2 | 2 |
|
| 3 | +import { Button } from '@onlook/ui/button'; |
| 4 | +import { Icons } from '@onlook/ui/icons'; |
| 5 | +import { Popover, PopoverContent, PopoverTrigger } from '@onlook/ui/popover'; |
| 6 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
3 | 7 | import { Border } from './dropdowns/border';
|
4 | 8 | import { ColorBackground } from './dropdowns/color-background';
|
5 | 9 | import { Display } from './dropdowns/display';
|
6 | 10 | import { Height } from './dropdowns/height';
|
7 | 11 | import { ImageBackground } from './dropdowns/img-background';
|
8 | 12 | import { Margin } from './dropdowns/margin';
|
| 13 | +import { Opacity } from './dropdowns/opacity'; |
9 | 14 | import { Padding } from './dropdowns/padding';
|
10 | 15 | import { Radius } from './dropdowns/radius';
|
11 | 16 | import { Width } from './dropdowns/width';
|
12 | 17 | import { ViewButtons } from './panels/panel-bar/bar';
|
13 | 18 | import { InputSeparator } from './separator';
|
14 | 19 |
|
15 |
| -export const DivSelected = () => { |
| 20 | +const COMPONENT_MAP: { [key: string]: any } = { |
| 21 | + Opacity, |
| 22 | + Width, |
| 23 | + Height, |
| 24 | + Display, |
| 25 | + Padding, |
| 26 | + Margin, |
| 27 | + Radius, |
| 28 | + Border, |
| 29 | + ColorBackground, |
| 30 | + ImageBackground, |
| 31 | + ViewButtons, |
| 32 | +}; |
| 33 | + |
| 34 | +// Group definitions for the div-selected toolbar |
| 35 | +export const DIV_SELECTED_GROUPS = [ |
| 36 | + { |
| 37 | + key: 'dimensions', |
| 38 | + label: 'Dimensions', |
| 39 | + components: ['Width', 'Height'], |
| 40 | + }, |
| 41 | + { |
| 42 | + key: 'base', |
| 43 | + label: 'Base', |
| 44 | + components: ['ColorBackground', 'Border', 'Radius'], |
| 45 | + }, |
| 46 | + { |
| 47 | + key: 'layout', |
| 48 | + label: 'Layout', |
| 49 | + components: ['Display', 'Padding', 'Margin'], |
| 50 | + }, |
| 51 | + { |
| 52 | + key: 'typography', |
| 53 | + label: 'Typography', |
| 54 | + components: ['FontFamily', 'FontWeight', 'FontSize', 'FontColor', 'TextAlign'], |
| 55 | + }, |
| 56 | + { |
| 57 | + key: 'opacity', |
| 58 | + label: 'Opacity', |
| 59 | + components: ['Opacity'], |
| 60 | + }, |
| 61 | +]; |
| 62 | + |
| 63 | +export const DivSelected = ({ availableWidth = 0 }: { availableWidth?: number }) => { |
| 64 | + const groupRefs = useRef<(HTMLDivElement | null)[]>([]); |
| 65 | + const [groupWidths, setGroupWidths] = useState<number[]>([]); |
| 66 | + const [overflowOpen, setOverflowOpen] = useState(false); |
| 67 | + const [visibleCount, setVisibleCount] = useState(DIV_SELECTED_GROUPS.length); |
| 68 | + |
| 69 | + // Calculate total width of a group including margins and padding |
| 70 | + const calculateGroupWidth = useCallback((element: HTMLElement | null): number => { |
| 71 | + if (!element) return 0; |
| 72 | + const style = window.getComputedStyle(element); |
| 73 | + const width = element.offsetWidth; |
| 74 | + const marginLeft = parseFloat(style.marginLeft); |
| 75 | + const marginRight = parseFloat(style.marginRight); |
| 76 | + const paddingLeft = parseFloat(style.paddingLeft); |
| 77 | + const paddingRight = parseFloat(style.paddingRight); |
| 78 | + return width + marginLeft + marginRight + paddingLeft + paddingRight; |
| 79 | + }, []); |
| 80 | + |
| 81 | + // Measure all group widths |
| 82 | + const measureGroups = useCallback(() => { |
| 83 | + const widths = groupRefs.current.map(ref => calculateGroupWidth(ref)); |
| 84 | + setGroupWidths(widths); |
| 85 | + }, [calculateGroupWidth]); |
| 86 | + |
| 87 | + // Update visible count based on available width |
| 88 | + const updateVisibleCount = useCallback(() => { |
| 89 | + if (!groupWidths.length || !availableWidth) return; |
| 90 | + |
| 91 | + const OVERFLOW_BUTTON_WIDTH = 32; // Reduced from 48px |
| 92 | + const MIN_GROUP_WIDTH = 80; // Reduced from 100px |
| 93 | + const SEPARATOR_WIDTH = 8; // Width of the InputSeparator |
| 94 | + let used = 0; |
| 95 | + let count = 0; |
| 96 | + |
| 97 | + for (let i = 0; i < groupWidths.length; i++) { |
| 98 | + const width = groupWidths[i] ?? 0; |
| 99 | + if (width < MIN_GROUP_WIDTH) continue; |
| 100 | + |
| 101 | + // Add separator width if this isn't the first group |
| 102 | + const totalWidth = width + (count > 0 ? SEPARATOR_WIDTH : 0); |
| 103 | + |
| 104 | + if (used + totalWidth <= availableWidth - OVERFLOW_BUTTON_WIDTH) { |
| 105 | + used += totalWidth; |
| 106 | + count++; |
| 107 | + } else { |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + setVisibleCount(count); |
| 113 | + }, [groupWidths, availableWidth]); |
| 114 | + |
| 115 | + // Measure group widths after mount and when groupRefs change |
| 116 | + useEffect(() => { |
| 117 | + measureGroups(); |
| 118 | + }, [measureGroups, availableWidth]); |
| 119 | + |
| 120 | + // Update visible count when measurements change |
| 121 | + useEffect(() => { |
| 122 | + updateVisibleCount(); |
| 123 | + }, [updateVisibleCount]); |
| 124 | + |
| 125 | + const visibleGroups = DIV_SELECTED_GROUPS.slice(0, visibleCount); |
| 126 | + const overflowGroups = DIV_SELECTED_GROUPS.slice(visibleCount); |
| 127 | + |
16 | 128 | return (
|
17 |
| - <div className="flex items-center gap-0.5"> |
18 |
| - {/* <StateDropdown /> */} |
19 |
| - <Width /> |
20 |
| - <Height /> |
21 |
| - <InputSeparator /> |
22 |
| - <Display /> |
23 |
| - <Padding /> |
24 |
| - <Margin /> |
25 |
| - <InputSeparator /> |
26 |
| - <Radius /> |
27 |
| - <Border /> |
28 |
| - <InputSeparator /> |
29 |
| - <ColorBackground /> |
30 |
| - <InputSeparator /> |
31 |
| - <ImageBackground /> |
32 |
| - <ViewButtons /> |
33 |
| - </div> |
| 129 | + <> |
| 130 | + {/* Hidden measurement container */} |
| 131 | + <div style={{ position: 'absolute', visibility: 'hidden', height: 0, overflow: 'hidden', pointerEvents: 'none' }}> |
| 132 | + {DIV_SELECTED_GROUPS.map((group, groupIdx) => ( |
| 133 | + <div |
| 134 | + key={group.key} |
| 135 | + className="flex items-center justify-center gap-0.5" |
| 136 | + ref={el => { groupRefs.current[groupIdx] = el; }} |
| 137 | + > |
| 138 | + {group.components.map((compKey, idx) => { |
| 139 | + const Comp = COMPONENT_MAP[compKey]; |
| 140 | + return Comp ? <Comp key={compKey + idx} /> : null; |
| 141 | + })} |
| 142 | + </div> |
| 143 | + ))} |
| 144 | + </div> |
| 145 | + <div |
| 146 | + className="flex items-center justify-center gap-0.5 w-full overflow-hidden" |
| 147 | + > |
| 148 | + {DIV_SELECTED_GROUPS.map((group, groupIdx) => ( |
| 149 | + groupIdx < visibleCount ? ( |
| 150 | + <React.Fragment key={group.key}> |
| 151 | + {groupIdx > 0 && <InputSeparator />} |
| 152 | + <div className="flex items-center justify-center gap-0.5"> |
| 153 | + {group.components.map((compKey, idx) => { |
| 154 | + const Comp = COMPONENT_MAP[compKey]; |
| 155 | + return Comp ? <Comp key={compKey + idx} /> : null; |
| 156 | + })} |
| 157 | + </div> |
| 158 | + </React.Fragment> |
| 159 | + ) : null |
| 160 | + ))} |
| 161 | + {overflowGroups.length > 0 && visibleCount > 0 && <InputSeparator />} |
| 162 | + {overflowGroups.length > 0 && ( |
| 163 | + <Popover open={overflowOpen} onOpenChange={setOverflowOpen}> |
| 164 | + <PopoverTrigger asChild> |
| 165 | + <Button |
| 166 | + variant="ghost" |
| 167 | + size="toolbar" |
| 168 | + className="w-8 h-8 flex items-center justify-center" |
| 169 | + aria-label="Show more toolbar controls" |
| 170 | + > |
| 171 | + <Icons.DotsHorizontal className="w-5 h-5" /> |
| 172 | + </Button> |
| 173 | + </PopoverTrigger> |
| 174 | + <PopoverContent align="end" className="flex flex-row gap-1 p-1 px-1 bg-background rounded-lg shadow-xl shadow-black/20 min-w-[fit-content] items-center w-[fit-content]"> |
| 175 | + {overflowGroups.map((group, groupIdx) => ( |
| 176 | + <React.Fragment key={group.key}> |
| 177 | + {groupIdx > 0 && <InputSeparator />} |
| 178 | + <div className="flex items-center gap-0.5"> |
| 179 | + {group.components.map((compKey, idx) => { |
| 180 | + const Comp = COMPONENT_MAP[compKey]; |
| 181 | + return Comp ? <Comp key={compKey + idx} /> : null; |
| 182 | + })} |
| 183 | + </div> |
| 184 | + </React.Fragment> |
| 185 | + ))} |
| 186 | + </PopoverContent> |
| 187 | + </Popover> |
| 188 | + )} |
| 189 | + </div> |
| 190 | + </> |
34 | 191 | );
|
35 | 192 | };
|
0 commit comments