Skip to content

[DONT MERGE] from meeting today #25

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Border = () => {
const { boxState, handleBoxChange, handleUnitChange, handleIndividualChange } = useBoxControl('border');
const editorEngine = useEditorEngine();

const [borderColor, setBorderColor] = useState(Color.from(editorEngine.style.getValue('borderColor') ?? '#080808').toHex());
const [borderColor, setBorderColor] = useState(Color.from(editorEngine.style.selectedStyle?.styles.borderColor ?? '#080808').toHex());

useEffect(() => {
setBorderColor(Color.from(editorEngine.style.getValue('borderColor') ?? '#080808').toHex());
Expand All @@ -32,7 +32,7 @@ export const Border = () => {
};

const borderStyle = {
borderWidth: boxState.border.num ? `1px`: '0px',
borderWidth: boxState.border.num ? `1px` : '0px',
borderStyle: 'solid',
};

Expand All @@ -46,10 +46,10 @@ export const Border = () => {
<Icons.BorderEdit className="h-4 w-4 min-h-4 min-w-4" />
<span className="text-sm">{boxState.border.value}</span>

<div
className="w-5 h-5 rounded-md"
style={borderStyle}
/>
<div
className="w-5 h-5 rounded-md"
style={borderStyle}
/>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="start" className="w-[280px] mt-1 p-3 rounded-lg">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,77 @@

import { Button } from "@onlook/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger,
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger,
} from "@onlook/ui/dropdown-menu";
import { Icons } from "@onlook/ui/icons";
import { useState } from "react";
import { useBoxControl } from "../hooks/use-box-control";
import { InputRange } from "../inputs/input-range";
import { SpacingInputs } from "../inputs/spacing-inputs";
import { useBoxControl } from "../hooks/use-box-control";

export const Margin = () => {
const [activeTab, setActiveTab] = useState("individual");
const { boxState, handleBoxChange, handleUnitChange, handleIndividualChange } = useBoxControl('margin');
const [activeTab, setActiveTab] = useState("individual");
const { boxState, handleBoxChange, handleUnitChange, handleIndividualChange, setIsUpdating } = useBoxControl('margin');

return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
className="text-muted-foreground border-border/0 hover:bg-background-tertiary/20 hover:border-border data-[state=open]:bg-background-tertiary/20 data-[state=open]:border-border flex cursor-pointer items-center gap-2 rounded-lg border px-3 hover:border hover:text-white focus:outline-none focus-visible:ring-0 focus-visible:ring-offset-0 focus-visible:outline-none active:border-0 data-[state=open]:border data-[state=open]:text-white"
>
<Icons.Margin className="h-4 min-h-4 w-4 min-w-4" />
<span className="text-sm">{boxState.margin.value}</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
className="mt-1 w-[280px] rounded-lg p-3"
>
<div className="mb-3 flex items-center gap-2">
<button
onClick={() => setActiveTab("all")}
className={`flex-1 cursor-pointer rounded-md px-4 py-1.5 text-sm transition-colors ${
activeTab === "all"
? "bg-background-tertiary/20 text-white"
: "text-muted-foreground hover:bg-background-tertiary/10"
}`}
>
All sides
</button>
<button
onClick={() => setActiveTab("individual")}
className={`flex-1 cursor-pointer rounded-md px-4 py-1.5 text-sm transition-colors ${
activeTab === "individual"
? "bg-background-tertiary/20 text-white"
: "text-muted-foreground hover:bg-background-tertiary/10"
}`}
>
Individual
</button>
</div>
{activeTab === "all" ? (
<InputRange
value={boxState.margin.num ?? 0}
onChange={(value) => handleBoxChange('margin', value.toString())}
unit={boxState.margin.unit}
onUnitChange={(unit) => handleUnitChange('margin', unit)}
/>
) : (
<SpacingInputs
type="margin"
values={{
top: boxState.marginTop.num ?? 0,
right: boxState.marginRight.num ?? 0,
bottom: boxState.marginBottom.num ?? 0,
left: boxState.marginLeft.num ?? 0
}}
onChange={handleIndividualChange}
/>
)}
</DropdownMenuContent>
</DropdownMenu>
);
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
className="text-muted-foreground border-border/0 hover:bg-background-tertiary/20 hover:border-border data-[state=open]:bg-background-tertiary/20 data-[state=open]:border-border flex cursor-pointer items-center gap-2 rounded-lg border px-3 hover:border hover:text-white focus:outline-none focus-visible:ring-0 focus-visible:ring-offset-0 focus-visible:outline-none active:border-0 data-[state=open]:border data-[state=open]:text-white"
>
<Icons.Margin className="h-4 min-h-4 w-4 min-w-4" />
<span className="text-sm">{boxState.margin.value}</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
className="mt-1 w-[280px] rounded-lg p-3"
>
<div className="mb-3 flex items-center gap-2">
<button
onClick={() => setActiveTab("all")}
className={`flex-1 cursor-pointer rounded-md px-4 py-1.5 text-sm transition-colors ${activeTab === "all"
? "bg-background-tertiary/20 text-white"
: "text-muted-foreground hover:bg-background-tertiary/10"
}`}
>
All sides
</button>
<button
onClick={() => setActiveTab("individual")}
className={`flex-1 cursor-pointer rounded-md px-4 py-1.5 text-sm transition-colors ${activeTab === "individual"
? "bg-background-tertiary/20 text-white"
: "text-muted-foreground hover:bg-background-tertiary/10"
}`}
>
Individual
</button>
</div>
{activeTab === "all" ? (
<InputRange
value={boxState.margin.num ?? 0}
onChange={(value) => handleBoxChange('margin', value.toString())}
unit={boxState.margin.unit}
onUnitChange={(unit) => handleUnitChange('margin', unit)}
onUnitChangeStart={() => setIsUpdating(true)}
onUnitChangeEnd={() => setIsUpdating(false)}
/>
) : (
<SpacingInputs
type="margin"
values={{
top: boxState.marginTop.num ?? 0,
right: boxState.marginRight.num ?? 0,
bottom: boxState.marginBottom.num ?? 0,
left: boxState.marginLeft.num ?? 0
}}
onChange={handleIndividualChange}
/>
)}
</DropdownMenuContent>
</DropdownMenu>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const createDefaultState = (type: BoxType): BoxStateMap => {

export const useBoxControl = (type: BoxType) => {
const editorEngine = useEditorEngine();
const [isUpdating, setIsUpdating] = useState(false);

const getInitialState = (): BoxStateMap => {
const defaultState = createDefaultState(type);
Expand Down Expand Up @@ -92,6 +93,8 @@ export const useBoxControl = (type: BoxType) => {
const [boxState, setBoxState] = useState<BoxStateMap>(getInitialState());

useEffect(() => {
console.log('isUpdating', isUpdating);
if (isUpdating) return;
setBoxState(getInitialState());
}, [editorEngine.style.selectedStyle]);

Expand Down Expand Up @@ -154,6 +157,8 @@ export const useBoxControl = (type: BoxType) => {
boxState,
handleBoxChange,
handleUnitChange,
handleIndividualChange
handleIndividualChange,
isUpdating,
setIsUpdating
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ interface InputRangeProps {
unit?: string;
onChange?: (value: number) => void;
onUnitChange?: (unit: string) => void;
onUnitChangeStart?: () => void;
onUnitChangeEnd?: () => void;
}

export const InputRange = ({ value, icon, unit = "px", onChange, onUnitChange }: InputRangeProps) => {
export const InputRange = ({ value, icon, unit = "px", onChange, onUnitChange, onUnitChangeStart, onUnitChangeEnd }: InputRangeProps) => {
const Icon = icon ? Icons[icon] : Icons.Padding;
const [inputValue, setInputValue] = useState(String(value));
const rangeRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -41,6 +43,7 @@ export const InputRange = ({ value, icon, unit = "px", onChange, onUnitChange }:

const handleMouseDown = (e: React.MouseEvent) => {
if (rangeRef.current) {
onUnitChangeStart?.();
setIsDragging(true);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
Expand All @@ -57,6 +60,7 @@ export const InputRange = ({ value, icon, unit = "px", onChange, onUnitChange }:
};

const handleMouseUp = () => {
onUnitChangeEnd?.();
setIsDragging(false);
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { makeAutoObservable, reaction } from 'mobx';
import type { EditorEngine } from '..';

export interface SelectedStyle {
styles: Record<string, string>;
styles: {
margin: string;
}
parentRect: DOMRect;
rect: DOMRect;
}
Expand Down
6 changes: 2 additions & 4 deletions apps/web/preload/script/api/elements/style.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { DomElementStyles } from '@onlook/models';
import { jsonClone } from '@onlook/utility';
import { getHtmlElement } from '../../helpers';

export function getStyles(element: HTMLElement): {
defined: Record<string, string>;
computed: Record<string, string>;
} {
export function getStyles(element: HTMLElement): DomElementStyles {
const computed = getElComputedStyle(element);
const inline = getInlineStyles(element);
const stylesheet = getStylesheetStyles(element);
Expand Down