Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

calculate picker position

Oleg Isonen3ee21d4aaa4f
diff --git a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
index 7c0659571c..a8608e16c1 100644
--- a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx
@@ -11,18 +11,30 @@ import type { ControlProps } from "../../style-sections";
import { getFinalValue } from "../../shared/get-final-value";
import { PropertyName } from "../../shared/property-name";
import { PANEL_WIDTH } from "~/designer/shared/constants";
+import { MutableRefObject, Ref, RefObject, useRef, useState } from "react";
const textFieldStyle = {
height: "$6",
fontWeight: "500",
};
+const usePickerSideOffset = (
+ isOpen: boolean
+): [MutableRefObject<HTMLInputElement | null>, number] => {
+ const ref = useRef<HTMLInputElement | null>(null);
+ const sideOffset =
+ isOpen && ref.current !== null ? PANEL_WIDTH - ref.current.offsetWidth : 0;
+ return [ref, sideOffset];
+};
+
export const FontFamilyControl = ({
currentStyle,
inheritedStyle,
//setProperty,
styleConfig,
}: ControlProps) => {
+ const [isOpen, setIsOpen] = useState(false);
+ const [textFieldRef, sideOffset] = usePickerSideOffset(isOpen);
// @todo show which instance we inherited the value from
const value = getFinalValue({
currentStyle,
@@ -38,15 +50,16 @@ export const FontFamilyControl = ({
<Grid columns={2}>
<PropertyName property={styleConfig.property} label={styleConfig.label} />
- <Popover>
- <PopoverTrigger asChild aria-label="Share project">
+ <Popover open={isOpen} onOpenChange={setIsOpen}>
+ <PopoverTrigger asChild>
<TextField
+ ref={textFieldRef}
css={textFieldStyle}
spellCheck={false}
readOnly
defaultValue={value.value}
- onClick={() => {
- // @todo show font picker
+ onClick={(event) => {
+ setIsOpen(true);
}}
onKeyDown={(event) => {
if (event.key === "Enter") {
@@ -56,12 +69,7 @@ export const FontFamilyControl = ({
/>
</PopoverTrigger>
<PopoverPortal>
- <PopoverContent
- //alignOffset={PANEL_WIDTH}
- sideOffset={PANEL_WIDTH}
- side="right"
- hideArrow
- >
+ <PopoverContent sideOffset={sideOffset} side="right" hideArrow>
<PopoverHeader title="Assets" />
</PopoverContent>
</PopoverPortal>