Show Content Hub icons

This example shows how to configure an External component to show and use Content Hub icons.

RequestResponse
import ReactDOM from "react-dom";
import IconComponent from "./IconComponent";

export default function createExternalRoot(container: HTMLElement) {
  return {
    render({
      icon,
    }: {
      icon: {
        insertContentHubIconInElement: (
          icon: string,
          htmlElement: HTMLElement
        ) => Promise<void>;
      };
    }) {
      const { insertContentHubIconInElement } = icon;

      ReactDOM.render(
        <div style={{ fontSize: "24px" }}>
          <IconComponent
            insertIconInElement={insertContentHubIconInElement}
            iconName="trash"
          />
          <IconComponent
            insertIconInElement={insertContentHubIconInElement}
            iconName="star"
          />
          <IconComponent
            insertIconInElement={insertContentHubIconInElement}
            iconName="star"
          />
        </div>,
        container
      );
    },
    unmount() {
      ReactDOM.unmountComponentAtNode(container);
    },
  };
}
RequestResponse
import { css } from "@emotion/css";
import { FC, useEffect, useRef, useState } from "react";

const svgStyles = css({
  lineHeight: 1,
  height: "1em",
  display: "inline-block",
  "& svg": {
    width: "1em",
    height: "1em",
    position: "sticky",
    zIndex: 1,
    verticalAlign: "bottom",
    userSelect: "none",
    fill: "currentColor",
    color: "currentColor",
  },
  "& path[stroke]": {
    stroke: "currentColor",
    fill: "transparent",
  },
});

interface IconComponentProps {
  insertIconInElement: (
    icon: string,
    htmlElement: HTMLElement
  ) => Promise<void>;
  iconName: string;
}

const IconComponent: FC<IconComponentProps> = ({
  insertIconInElement,
  iconName,
}) => {
  const iconRef = useRef<HTMLElement | null>(null);

  useEffect(() => {
    const loadSvgIcon = async (): Promise<void> => {
      if (iconRef.current) {
        await insertIconInElement(iconName, iconRef.current);
      }
    };

    if (iconName) {
      void loadSvgIcon();
    }
  }, [iconName]);

  return <i ref={iconRef} className={svgStyles} />;
};

export default IconComponent;

Do you have some feedback for us?

If you have suggestions for improving this article,