refactor: add interface each component to define props config in JSON file

This commit is contained in:
Nam Doan 2025-11-26 11:22:08 +07:00
parent 3e5a88f42d
commit 60700ee11e
7 changed files with 100 additions and 26 deletions

View File

@ -2,8 +2,13 @@ import LocalizedClientLink from "@modules/common/components/localized-client-lin
import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map"; import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map";
import { clx } from "@medusajs/ui" import { clx } from "@medusajs/ui"
interface VtLinkProps {
className?: string
href?: string
label?: string
}
export const VtLink = ({ nodes, context }: { nodes: LayoutComponentDefinition; context: LayoutContext }) => { export const VtLink = ({ nodes, context }: { nodes: LayoutComponentDefinition; context: LayoutContext }) => {
const props = nodes.config ?? {} const props = nodes.config as VtLinkProps ?? {}
const className = clx("txt-compact-xlarge-plus hover:text-ui-fg-base", props.className) const className = clx("txt-compact-xlarge-plus hover:text-ui-fg-base", props.className)
const href = props.href ?? "/" const href = props.href ?? "/"
const label = props.label ?? "Medusa Store" const label = props.label ?? "Medusa Store"

View File

@ -12,8 +12,15 @@ interface BannerCTAProps {
context: LayoutContext context: LayoutContext
} }
interface BannerConfigProps {
text?: string
href?: string
iconLeft?: string
iconRight?: string
className?: string
}
export default function BannerCTA({ node, context }: BannerCTAProps) { export default function BannerCTA({ node, context }: BannerCTAProps) {
const props = node.config ?? {} const props = node.config as BannerConfigProps ?? {}
const text = props.text ?? "" const text = props.text ?? ""
const href = props.href ?? undefined const href = props.href ?? undefined
const iconLeft = props.iconLeft const iconLeft = props.iconLeft

View File

@ -1,8 +1,13 @@
import { DynamicLayoutRenderer } from "vibentec/renderer" import { DynamicLayoutRenderer } from "vibentec/renderer"
import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map"; import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map";
export default function BannerNav({ node: node, context }: { node: LayoutComponentDefinition; context: LayoutContext }) { interface BannerNavProps {
const props = node.config ?? {}; left?: LayoutComponentDefinition[]
center?: LayoutComponentDefinition[]
right?: LayoutComponentDefinition[]
}
export default function BannerNav({ node, context }: { node: LayoutComponentDefinition; context: LayoutContext }) {
const props = node.config as BannerNavProps ?? {};
return ( return (
<nav className="content-container txt-xsmall-plus text-ui-fg-subtle flex items-center justify-between w-full h-full text-small-regular"> <nav className="content-container txt-xsmall-plus text-ui-fg-subtle flex items-center justify-between w-full h-full text-small-regular">

View File

@ -2,13 +2,21 @@ import styles from "./banner-ticker.module.css"
import { DynamicLayoutRenderer } from "vibentec/renderer" import { DynamicLayoutRenderer } from "vibentec/renderer"
import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map" import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map"
export default function BannerTicker({ node, context }: { node: LayoutComponentDefinition; context: LayoutContext }) { interface BannerTickerProps {
const props = node.config ?? {} node: LayoutComponentDefinition
context: LayoutContext
}
interface BannerConfigProps {
items?: LayoutComponentDefinition[]
speed?: number
}
export default function BannerTicker({ node, context }: BannerTickerProps) {
const props = node.config as BannerConfigProps ?? {}
const speed = props.speed ?? 10; const speed = props.speed ?? 10;
return ( return (
<div className="relative overflow-hidden w-full h-full"> <div className="relative overflow-hidden w-full h-full">
<div className={styles.ticker} style={{ animationDuration: `${speed}s` }} > <div className={styles.ticker} style={{ animationDuration: `${speed}s` }} >
<DynamicLayoutRenderer nodes={props.items} context={context} /> <DynamicLayoutRenderer nodes={props.items ?? []} context={context} />
</div> </div>
</div> </div>
) )

View File

@ -1,26 +1,42 @@
import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map"; import {
import { clx } from "@medusajs/ui"; LayoutComponentDefinition,
import BannerNav from "./banner-nav"; LayoutContext,
import BannerCTA from "./banner-cta"; } from "vibentec/component-map"
import BannerTicker from "./banner-ticker"; import { clx } from "@medusajs/ui"
import BannerNav from "./banner-nav"
import BannerCTA from "./banner-cta"
import BannerTicker from "./banner-ticker"
export default async function Banner({ nodes, context }: { nodes: LayoutComponentDefinition; context: LayoutContext }) { interface BannerProps {
const props = nodes.config ?? {}; variant: "nav" | "cta" | "ticker"
const bannerClassName = clx("relative h-8 mx-auto border-b duration-200 bg-white border-ui-border-base", props.className); className?: string
const bannerVariant = props.variant as "nav" | "cta" | "ticker"; speed?: number
if (!bannerVariant) return null; }
export default async function Banner({
nodes,
context,
}: {
nodes: LayoutComponentDefinition
context: LayoutContext
}) {
const props = (nodes.config as BannerProps) ?? {}
const bannerClassName = clx(
"relative h-8 mx-auto border-b duration-200 bg-white border-ui-border-base",
props.className
)
if (!props.variant) return null
const variants = { const variants = {
"nav": BannerNav, nav: BannerNav,
"cta": BannerCTA, cta: BannerCTA,
"ticker": BannerTicker, ticker: BannerTicker,
}; }
const Component = variants[bannerVariant]; const Component = variants[props.variant]
return ( return (
<div className={bannerClassName}> <div className={bannerClassName}>
<Component node={nodes} context={context}/> <Component node={nodes} context={context} />
</div> </div>
); )
} }

View File

@ -0,0 +1,27 @@
import { clx } from "@medusajs/ui"
import {
LayoutComponentDefinition,
LayoutContext,
} from "@vibentec/component-map"
import Image from "next/image"
export interface VtImageConfig {
src: string
alt: string
className?: string,
objectFit?: string,
}
export default function VtImage({
nodes,
context,
}: {
nodes: LayoutComponentDefinition
context: LayoutContext
}) {
const props = (nodes.config as VtImageConfig) ?? {}
return (
<div className={clx("relative", props.className)}>
<Image src={props.src} alt={props.alt} fill objectFit={props.objectFit ?? "contain"} />
</div>
)
}

View File

@ -1,11 +1,17 @@
import { DynamicLayoutRenderer } from "vibentec/renderer" import { DynamicLayoutRenderer } from "vibentec/renderer"
import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map"; import { LayoutComponentDefinition, LayoutContext } from "vibentec/component-map";
interface BannerNavProps {
className?: string;
left?: LayoutComponentDefinition[];
center?: LayoutComponentDefinition[];
right?: LayoutComponentDefinition[];
}
export default function VtNav({ nodes, context }: { nodes: LayoutComponentDefinition; context: LayoutContext }) { export default function VtNav({ nodes, context }: { nodes: LayoutComponentDefinition; context: LayoutContext }) {
const props = nodes.config ?? {} const props = nodes.config as BannerNavProps ?? {}
return ( return (
<div className="relative h-16 mx-auto border-b duration-200 bg-white border-ui-border-base"> <div className="relative mx-auto border-b duration-200 bg-white border-ui-border-base">
<nav className="content-container txt-xsmall-plus text-ui-fg-subtle flex items-center justify-between w-full h-full text-small-regular"> <nav className="content-container txt-xsmall-plus text-ui-fg-subtle flex items-center justify-between w-full h-full text-small-regular">
<div className="flex items-center gap-x-4"> <div className="flex items-center gap-x-4">
{props.left && <DynamicLayoutRenderer nodes={props.left} context={context} />} {props.left && <DynamicLayoutRenderer nodes={props.left} context={context} />}