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.

1.2 KB
Raw Blame
import { useStore } from "@nanostores/react";
import {
  selectorIdAttribute,
  type AnyComponent,
  type WebstudioComponentSystemProps,
} from "@webstudio-is/react-sdk";
import * as React from "react";
import { $isDesignMode } from "~/shared/nano-states";
import { $selectedInstanceSelector } from "~/shared/nano-states";

export const BlockTemplate = React.forwardRef<
  HTMLDivElement,
  WebstudioComponentSystemProps & { children: React.ReactNode }
>(({ ...props }, ref) => {
  const isDesignMode = useStore($isDesignMode);
  const selectedInstanceSelector = useStore($selectedInstanceSelector);
  const templateInstanceStringSelector = props[selectorIdAttribute];

  if (!isDesignMode) {
    return;
  }

  if (selectedInstanceSelector === undefined) {
    return;
  }

  if (selectedInstanceSelector.length === 0) {
    return;
  }

  const selectedSelector = selectedInstanceSelector.join(",");

  // Exclude all selected ancestors
  if (!selectedSelector.endsWith(templateInstanceStringSelector)) {
    return;
  }

  const childrenCount = React.Children.count(props.children);

  return (
    <div
      style={{ display: childrenCount === 0 ? "block" : "contents" }}
      ref={ref}
      {...props}
    />
  );
}) as AnyComponent;