import React, { type ChangeEvent, createElement as reactCreateElement, forwardRef, useImperativeHandle, useMemo, useRef, } from "react"; import { BaseFrameUI, type BaseFrameUIProps } from "./frame.base"; import type { FrameRootContainerProps, FrameUIComponents, RootContainerElement, } from "./types"; export type { FrameUIComponents, FrameUITheme } from "./frame.base"; declare module "react" { interface CSSProperties { "--frame-image-aspect-ratio"?: string; } } type StylingProps = { className?: string; style?: React.CSSProperties; }; const RootContainer = forwardRef< RootContainerElement, FrameRootContainerProps & StylingProps >( ( { createElement, buttonsContainer, imageContainer, loadingScreen, textInputContainer, dimensions, className, style, }, ref ) => { const elementRef = useRef(null); useImperativeHandle(ref, () => { return { computeDimensions() { if (!elementRef.current) { return undefined; } const { width, height } = elementRef.current.getBoundingClientRect(); return { width, height }; }, }; }); return createElement( "div", { className, ref: elementRef, style: { ...dimensions, ...style }, }, loadingScreen, imageContainer, textInputContainer, buttonsContainer ); } ); RootContainer.displayName = "RootContainer"; function isCssProperties(value: unknown): value is React.CSSProperties { return typeof value === "object" && value !== null; } function createDefaultComponents>( createElement = reactCreateElement ): FrameUIComponents { return { Button(props, stylingProps) { const label = ( <> {props.frameButton.action === "mint" && "⬗ "} {props.frameButton.label} {props.frameButton.action === "tx" && ( )} {(props.frameButton.action === "post_redirect" || props.frameButton.action === "link") && ` ↗`} ); return createElement( "button", { ...stylingProps, key: props.index, disabled: props.isDisabled, onClick: props.onPress, type: "button", }, label ); }, ButtonsContainer(props, stylingProps) { return createElement("div", stylingProps, props.buttons); }, Error(props, stylingProps) { return createElement("div", stylingProps, props.message); }, Image(props, stylingProps) { const aspectRatio = props.aspectRatio.replace(":", "/"); let sanitizedSrc = props.status === "frame-loading" ? undefined : props.src; // Don't allow data URLs that are not images -- we don't want to allow arbitrary data to be loaded if ( sanitizedSrc?.startsWith("data:") && !sanitizedSrc?.startsWith("data:image") ) { sanitizedSrc = undefined; } // Don't allow SVG data URLs -- could contain malicious code if (sanitizedSrc?.startsWith("data:image/svg")) { sanitizedSrc = undefined; } return createElement("img", { ...stylingProps, "data-aspect-ratio": aspectRatio, style: { "--frame-image-aspect-ratio": aspectRatio, ...(isCssProperties(stylingProps.style) && stylingProps.style), }, onLoad: props.onImageLoadEnd, onError: props.onImageLoadEnd, src: sanitizedSrc, alt: "Frame", }); }, ImageContainer(props, stylingProps) { const aspectRatio = props.aspectRatio.replace(":", "/"); return createElement( "div", { ...stylingProps, style: { "--frame-image-aspect-ratio": aspectRatio, ...(isCssProperties(stylingProps.style) && stylingProps.style), }, }, props.image, props.messageTooltip ); }, LoadingScreen(props, stylingProps) { return createElement("div", stylingProps, "Loading..."); }, Message(props, stylingProps) { return createElement( "div", { ...stylingProps, "data-status": props.status, }, props.message ); }, MessageTooltip(props, stylingProps) { return createElement( "div", { ...stylingProps, "data-status": props.status }, props.message ); }, Root(props, stylingProps) { return createElement(RootContainer, { ...props, ...stylingProps }); }, TextInput(props, stylingProps) { return createElement("input", { ...stylingProps, disabled: props.isDisabled, onChange: (e: ChangeEvent) => { props.onChange(e.target.value); }, placeholder: props.placeholder, value: props.value, type: "text", }); }, TextInputContainer(props, stylingProps) { return createElement("div", stylingProps, props.textInput); }, }; } type FrameUIProps> = Omit< BaseFrameUIProps, "components" > & { /** * Components should be memoized to avoid re-rendering the entire UI */ components?: Partial>; }; export function FrameUI< TStylingProps extends Record = StylingProps >(props: FrameUIProps): JSX.Element { const defaultComponents = useMemo( () => createDefaultComponents(props.createElement), [props.createElement] ); const components = useMemo(() => { return { ...defaultComponents, ...props.components, }; }, [defaultComponents, props.components]); return {...props} components={components} />; }