cschroeter / park-ui

Beautifully designed components built on Ark UI that work for the JS and CSS frameworks of your choice.
https://park-ui.com
MIT License
1.47k stars 59 forks source link

Nextjs Eslint Error , Component definition is missing display name #331

Open wadoud1234 opened 1 month ago

wadoud1234 commented 1 month ago

I was facing an error when build nextjs app for production this error appears as some components doesn't have display name Screenshot from 2024-06-01 19-12-14

this error was related to file create-style-context.tsx in lib directory

wadoud1234 commented 1 month ago

As solution for this i did some modification to the code , to add display name to components created by arrow functions , first i create the component , then i forwardref to it , the old code `

type Recipe = (props: Record<string, unknown>) => Record<string, () => string>;
type Slot<R extends Recipe> = keyof ReturnType<R>;

const cx = (...args: (string | undefined)[]) => args.filter(Boolean).join(" ");

export const createStyleContext = <R extends Recipe>(recipe: R) => {
const StyleContext = createContext<Record<Slot<R>, () => string> | null>(
  null
);

const withRootProvider = <P extends {}>(Component: ElementType) => {
  const StyledComponent = (props: P) => {
    const slotStyles = recipe(props) as Record<Slot<R>, () => string>;

    return (
      <StyleContext.Provider value={slotStyles}>
        <Component {...props} />
      </StyleContext.Provider>
    );
  };
  return StyledComponent;
};
withRootProvider.displayName = "withRootProvider";
const withProvider = <T, P extends { className?: string }>(
  Component: ElementType,
  slot: Slot<R>
) => {
  return forwardRef<T, P>((props, ref) => {
    const slotStyles = recipe(props) as Record<Slot<R>, () => string>;
    return (
      <StyleContext.Provider value={slotStyles}>
        <Component
          {...props}
          ref={ref}
          className={cx(slotStyles?.[slot](), props.className)}
        />
      </StyleContext.Provider>
    );
  });
};
withProvider.displayName = "withContext";
const withContext = <T, P extends { className?: string }>(
  Component: ElementType,
  slot: Slot<R>
): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => {
  return forwardRef<T, P>((props, ref) => {
    const slotStyles = useContext(StyleContext);
    return (
      <Component
        {...props}
        ref={ref}
        className={cx(slotStyles?.[slot](), props.className)}
      />
    );
  });
};
withContext.displayName = "withContext";
return {
  withRootProvider,
  withProvider,
  withContext,
};

};

`

new fixed code `

type Recipe = (props: Record<string, unknown>) => Record<string, () => string>;
type Slot<R extends Recipe> = keyof ReturnType<R>;

const cx = (...args: (string | undefined)[]) => args.filter(Boolean).join(" ");

export const createStyleContext = <R extends Recipe>(recipe: R) => {
  const StyleContext = createContext<Record<Slot<R>, () => string> | null>( null );

const withRootProvider = <P extends {}>(Component: ElementType) => {
  const StyledComponent = (props: P) => {
    const slotStyles = recipe(props) as Record<Slot<R>, () => string>;

    return (
      <StyleContext.Provider value={slotStyles}>
        <Component {...props} />
      </StyleContext.Provider>
    );
  };
  return StyledComponent;
};
withRootProvider.displayName = "withRootProvider";
const withProvider = <T, P extends { className?: string }>(
  Component: ElementType,
  slot: Slot<R>
) => {
  const ChildComponent = (props: P, ref: ForwardedRef<T>) => {
    const slotStyles = recipe(props) as Record<Slot<R>, () => string>;
    return (
      <StyleContext.Provider value={slotStyles}>
        <Component
          {...props}
          ref={ref}
          className={cx(slotStyles?.[slot](), props.className)}
        />
      </StyleContext.Provider>
    );
  };
  return forwardRef<T, P>(ChildComponent);
};
withProvider.displayName = "withContext";
const withContext = <T, P extends { className?: string }>(
  Component: ElementType,
  slot: Slot<R>
): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => {
  const ChildComponent = (props: P, ref: ForwardedRef<T>) => {
    const slotStyles = useContext(StyleContext);
    return (
      <Component
        {...props}
        ref={ref}
        className={cx(slotStyles?.[slot](), props.className)}
      />
    );
  };
  return forwardRef(ChildComponent);
};
withContext.displayName = "withContext";
return {
  withRootProvider,
  withProvider,
  withContext,
};

};

createStyleContext.dislayName = "createStyleContext"; `

corydeppen commented 1 week ago

This same error appears when using a component like Text. I also noticed the Next quick start example has the react/display-name rule turned off.

Is this a known issue that may be resolved in a future release?