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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
don't ignore nested lib dirs
Oleg Isonen62158f7df531
diff --git a/.gitignore b/.gitignoreindex ff9ea46e79..63228a2201 100644--- a/.gitignore+++ b/.gitignore@@ -1,5 +1,5 @@node_modules-lib+/lib.DS_Store.envyarn-error.logdiff --git a/app/designer/style-panel/lib/autocomplete.tsx b/app/designer/style-panel/lib/autocomplete.tsxnew file mode 100644index 0000000000..3a2bc9590a--- /dev/null+++ b/app/designer/style-panel/lib/autocomplete.tsx@@ -0,0 +1,227 @@+import {+ useState,+ useRef,+ useCallback,+ useMemo,+ type ComponentProps,+ useEffect,+} from "react";+import useDebounce from "react-use/lib/useDebounce";+import {+ TextField,+ MenuAnchor,+ Menu,+ MenuContent,+ MenuItem,+} from "~/shared/design-system";++type Items = Array<{ label: string }>;++const findIndex = (items: Items, value: string, exact = false): number => {+ const item = items.find(({ label }) => {+ return exact ? label === value : label.includes(value);+ });+ if (item === undefined) return -1;+ return items.indexOf(item);+};++type AutocompleteProps = {+ items: Items;+ onChangeValue: (value: string) => void;+ value: string;+} & ComponentProps<typeof TextField>;++export const Autocomplete = ({+ items,+ onChangeValue,+ value,+ ...rest+}: AutocompleteProps) => {+ const [debouncedValue, setDebouncedValue] = useState<string>();+ const [isOpen, setIsOpen] = useState<boolean>();+ const inputRef = useRef<HTMLInputElement>(null);+ const contentRef = useRef<HTMLDivElement>();+ const isMouseInRef = useRef<boolean>(false);+ const previousValueRef = useRef<string>(value);++ useEffect(() => {+ if (isOpen === false) {+ // Remember the value we had before opening autocomplete+ // so that when we press ESC we can restore it.+ previousValueRef.current = value;+ }+ }, [value, isOpen]);++ const [, cancelDebounce] = useDebounce(+ () => {+ if (debouncedValue !== undefined) {+ select(debouncedValue);+ }+ },+ 500,+ [debouncedValue]+ );++ const select = (nextValue?: string) => {+ cancelDebounce();+ if (nextValue !== undefined && nextValue !== value) {+ onChangeValue(nextValue);+ }+ };++ const open = () => {+ if (isOpen !== true && currentIndex !== -1) setIsOpen(true);+ };++ const abort = () => {+ if (previousValueRef.current !== undefined) {+ select(previousValueRef.current);+ }+ close();+ };++ const close = useCallback(() => {+ setDebouncedValue(undefined);+ cancelDebounce();+ setIsOpen(false);+ }, [cancelDebounce]);++ const currentIndex = useMemo(() => {+ // We try to match exact item first, then non-exact as a fallback.+ const exactMatchIndex = findIndex(items, value, true);+ const index =+ exactMatchIndex === -1 ? findIndex(items, value) : exactMatchIndex;++ return index;+ }, [items, value]);++ useEffect(() => {+ if (isOpen === false) return;+ if (currentIndex === -1) {+ close();+ return;+ }++ // We don't want to auto scroll when pointer is on the menu+ if (contentRef.current && isMouseInRef.current === false) {+ contentRef.current.children[currentIndex]?.scrollIntoView({+ block: "center",+ });+ }+ }, [currentIndex, isOpen, close]);++ const contentRefCallback = useCallback(+ (contentElement) => {+ contentRef.current = contentElement;+ // When we open with an arrow+ // menu catches the key event and focuses the first item,+ // which triggers onFocus handler.+ // We need to prevent that handler from executing.+ cancelDebounce();++ if (contentElement && inputRef.current !== null) {+ inputRef.current.focus();+ }+ },+ [inputRef, cancelDebounce]+ );++ return (+ <Menu open={isOpen} modal={false}>+ <MenuAnchor asChild>+ <TextField+ {...rest}+ value={value}+ ref={inputRef}+ autoComplete="off"+ onClick={() => {+ open();+ }}+ onFocus={() => {+ inputRef.current?.select();+ if (isOpen === undefined) open();+ }}+ onChange={(event) => {+ const value = event.target.value.trim();+ select(value);+ open();+ onChangeValue(value);+ }}+ onKeyDown={(event) => {+ switch (event.key) {+ case "Escape":+ event.preventDefault();+ abort();+ break;+ case "Enter": {+ event.preventDefault();+ select(items[currentIndex]?.label);+ isOpen ? close() : open();+ break;+ }+ case "ArrowDown": {+ event.preventDefault();+ if (isOpen === false) {+ open();+ break;+ }+ const index = items[currentIndex + 1] ? currentIndex + 1 : 0;+ select(items[index]?.label);+ break;+ }+ case "ArrowUp": {+ event.preventDefault();+ if (isOpen === false) {+ open();+ break;+ }+ const index = items[currentIndex - 1]+ ? currentIndex - 1+ : items.length - 1;+ select(items[index]?.label);+ break;+ }+ }+ }}+ />+ </MenuAnchor>+ <MenuContent+ loop+ portalled+ css={{ maxHeight: 200, overflow: "auto" }}+ asChild+ onMouseEnter={() => {+ isMouseInRef.current = true;+ }}+ onMouseLeave={() => {+ isMouseInRef.current = false;+ }}+ onPointerDownOutside={() => {+ abort();+ }}+ >+ <div ref={contentRefCallback}>+ {items.map(({ label }, index) => {+ return (+ <MenuItem+ key={index}+ {...(index === currentIndex+ ? { "data-found": true }+ : undefined)}+ onSelect={() => {+ select(label);+ close();+ }}+ onFocus={() => {+ setDebouncedValue(items[index]?.label);+ }}+ >+ {label}+ </MenuItem>+ );+ })}+ </div>+ </MenuContent>+ </Menu>+ );+};diff --git a/app/designer/style-panel/lib/color-picker.tsx b/app/designer/style-panel/lib/color-picker.tsxnew file mode 100644index 0000000000..8421520697--- /dev/null+++ b/app/designer/style-panel/lib/color-picker.tsx@@ -0,0 +1,69 @@+import { useState } from "react";+import { ColorResult, RGBColor, SketchPicker } from "react-color";+import {+ Box,+ Flex,+ Popover,+ PopoverTrigger,+ PopoverContent,+ TextField,+} from "~/shared/design-system";++const stringifyRGBA = (color: RGBColor) => {+ const { r, g, b, a = 1 } = color;++ return `rgba(${r},${g},${b},${a})`;+};++type ColorPickerProps = {+ onChangeValue: (value: string) => void;+ value: string;+};++export const ColorPicker = ({ value, onChangeValue }: ColorPickerProps) => {+ const [displayColorPicker, setDisplayColorPicker] = useState(false);++ return (+ <Popover+ modal+ open={displayColorPicker}+ onOpenChange={setDisplayColorPicker}+ >+ <PopoverTrigger asChild aria-label="Open color picker">+ <Flex>+ <Box+ css={{+ width: "$5",+ height: "$5",+ background: value,+ }}+ />+ <TextField+ onChange={(e) => onChangeValue(e.target.value)}+ onClick={() => setDisplayColorPicker((shown) => !shown)}+ variant="ghost"+ value={value}+ />+ </Flex>+ </PopoverTrigger>++ <PopoverContent>+ <SketchPicker+ color={value}+ onChange={(color: ColorResult) =>+ onChangeValue(stringifyRGBA(color.rgb))+ }+ // @todo to remove both when we have preset colors+ presetColors={[]}+ styles={{+ default: {+ picker: {+ padding: 10,+ },+ },+ }}+ />+ </PopoverContent>+ </Popover>+ );+};diff --git a/app/designer/style-panel/lib/spacing-widget.tsx b/app/designer/style-panel/lib/spacing-widget.tsxnew file mode 100644index 0000000000..fb99436f4c--- /dev/null+++ b/app/designer/style-panel/lib/spacing-widget.tsx@@ -0,0 +1,203 @@+import { Box } from "~/shared/design-system";+import {+ buildCss,+ type StyleProperty,+ type StyleValue,+} from "@webstudio-is/sdk";+import { SetProperty } from "../use-style-data";++type SpacingSingularStyle = { [property in SpacingProperty]?: StyleValue };++export type SpacingStyles = {+ paddings: SpacingSingularStyle;+ margins: SpacingSingularStyle;+};++export type SpacingProperty = Margin | Padding;++type Margin = "marginTop" | "marginRight" | "marginBottom" | "marginLeft";++type Padding = "paddingTop" | "paddingRight" | "paddingBottom" | "paddingLeft";++const grid = {+ margin: {+ marginTop: "1 / 2 / 2 / 3",+ marginRight: "2 / 3 / 3 / 4",+ marginBottom: "3 / 2 / 4 / 3",+ marginLeft: "2 / 1 / 3 / 2",+ },+ padding: {+ paddingTop: "1 / 2 / 2 / 3",+ paddingRight: "2 / 3 / 3 / 4",+ paddingBottom: "3 / 2 / 4 / 3",+ paddingLeft: "2 / 1 / 3 / 2",+ },+};++const styles = {+ spacingEdit: {+ fontSize: "$1",+ // @todo use a color from design system+ color: "rgb(217, 217, 217)",+ display: "flex",+ alignItems: "center",+ justifyContent: "center",+ },+ wrapper: {+ display: "grid",+ height: 130,+ gridTemplateColumns: "$5 $1 $5 1fr $5 $1 $5",+ gridTemplateRows: "$5 $1 $5 1fr $5 $1 $5",+ },+ input: {+ fontSize: 10,+ fontWeight: 400,+ fontFamily: "inherit",+ display: "block",+ background: "transparent",+ color: "$hiContrast",+ zIndex: 99,+ margin: "auto",+ // @todo need to fit more chars+ width: 40,+ border: "none",+ textAlign: "center",+ outline: "none",+ },+ emptySpace: {+ gridArea: "2 / 2 / 3 / 2",+ background: "$loContrast",+ width: "$6",+ margin: "auto",+ height: "100%",+ borderRadius: "$1",+ },+ marginGrid: {+ gridArea: "1 / 1 / -1 / -1",+ display: "grid",+ gridTemplateColumns: "$5 1fr $5",+ gridTemplateRows: "$5 minmax($3, 1fr) $5",+ height: 130,+ backgroundColor: "$gray6",+ borderRadius: "$1",+ px: 2,+ },+ text: {+ fontWeight: "bold",+ color: "$gray12",+ fontSize: 8,+ margin: "$1",+ },+ paddingGrid: {+ gridArea: "3 / 3 / span 3 / span 3",+ display: "grid",+ gridTemplateColumns: "$5 1fr $5",+ gridTemplateRows: "$5 minmax($3, 1fr) $5",+ border: "2px solid",+ borderColor: "$loContrast",+ borderRadius: "$1",+ },+};++const cleanValueForCSS = (value: string) => {+ const isNum = /^\d+$/.test(value);+ const isOnlyText = /^[A-Za-z]+$/.test(value);++ if (isNum) return `${value.toString()}px`;+ if (isOnlyText) return "0px";++ return value;+};++type SpacingWidgetProps = {+ setProperty: SetProperty;+ values: SpacingStyles;+};++export const SpacingWidget = ({ setProperty, values }: SpacingWidgetProps) => {+ const margins = buildCss(values.margins);+ const paddings = buildCss(values.paddings);++ const updateSpacing = ({+ value,+ property,+ }: {+ value: string;+ property: StyleProperty;+ }) => {+ setProperty(property)(cleanValueForCSS(value));+ };++ return (+ <Box css={styles.wrapper}>+ <Box css={styles.marginGrid}>+ {(Object.keys(margins) as Array<Margin>).map((property: Margin) => (+ <Box+ key={property}+ css={{+ ...styles.spacingEdit,+ gridArea: grid.margin[property],+ }}+ >+ <Box+ as="input"+ name={property}+ aria-label={`${property} edit`}+ value={margins[property]?.toString()}+ onChange={(event: React.ChangeEvent<HTMLInputElement>) =>+ updateSpacing({+ value: event.target.value,+ property,+ })+ }+ css={styles.input}+ />+ </Box>+ ))}+ </Box>+ <Box css={styles.paddingGrid}>+ {(Object.keys(paddings) as Array<Padding>).map((property: Padding) => (+ <Box+ key={property}+ css={{+ ...styles.spacingEdit,+ gridArea: grid.padding[property],+ }}+ >+ <Box+ as="input"+ name={property}+ aria-label={`${property} edit`}+ value={paddings[property]?.toString()}+ onChange={(event: React.ChangeEvent<HTMLInputElement>) =>+ updateSpacing({+ value: event.target.value,+ property,+ })+ }+ css={styles.input}+ />+ </Box>+ ))}+ <Box css={styles.emptySpace} />+ </Box>+ <Box+ css={{+ ...styles.text,+ gridArea: "3 / 3 / span 3 / span 3",+ }}+ >+ PADDING+ </Box>++ <Box+ css={{+ ...styles.text,+ gridArea: "1 / 1 / -1 / -1",+ }}+ >+ MARGIN+ </Box>+ </Box>+ );+};