import { CtaField } from '@components/common/page-builder/fields/CtaField.js';
import type { CtaValue } from '@components/common/page-builder/fields/CtaField.js';
import {
drawerInputClass,
Field,
ImagePickerField,
MarkdownBodyField,
Section,
Segmented,
useScopedFormContext
} from '@components/common/page-builder/index.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import {
AlignEndVertical,
AlignStartVertical,
AlignVerticalSpaceAround
} from 'lucide-react';
import React from 'react';
import type {
SplitImageFit,
SplitImagePosition,
SplitVerticalAlign
} from './SplitFeature.js';
interface SplitFeatureSettingProps {
splitFeatureWidget?: {
image?: string;
imageAlt?: string;
imagePosition?: SplitImagePosition;
width?: number | null;
height?: number | null;
eyebrow?: string | null;
heading?: string;
body?: string | null;
cta?: CtaValue | null;
verticalAlign?: SplitVerticalAlign;
imageFit?: SplitImageFit;
};
}
const POSITION_OPTIONS: ReadonlyArray<{
value: SplitImagePosition;
label: string;
}> = [
{ value: 'left', label: _('Image left') },
{ value: 'right', label: _('Image right') }
];
const ALIGN_OPTIONS: ReadonlyArray<{
value: SplitVerticalAlign;
label: string;
icon: React.ReactNode;
}> = [
{ value: 'top', label: _('Top'), icon: },
{
value: 'center',
label: _('Center'),
icon:
},
{ value: 'bottom', label: _('Bottom'), icon: }
];
const FIT_OPTIONS: ReadonlyArray<{ value: SplitImageFit; label: string }> = [
{ value: 'cover', label: _('Cover') },
{ value: 'contain', label: _('Contain') }
];
const BLANK_CTA: CtaValue = {
label: 'Shop the drop',
url: '/',
kind: 'custom',
newTab: false,
style: 'primary'
};
export default function SplitFeatureSetting({
splitFeatureWidget
}: SplitFeatureSettingProps) {
const {
image,
imageAlt,
imagePosition,
width,
height,
eyebrow,
heading,
body,
cta,
verticalAlign,
imageFit
} = splitFeatureWidget ?? {};
const { register, setValue, watch } = useScopedFormContext();
const imageV = (watch('settings.image') as string) ?? image ?? '';
const imageAltV = (watch('settings.imageAlt') as string) ?? imageAlt ?? '';
const imagePositionV =
((watch('settings.imagePosition') as string) ??
imagePosition ??
'left') as SplitImagePosition;
const eyebrowV = (watch('settings.eyebrow') as string) ?? eyebrow ?? '';
const headingV = (watch('settings.heading') as string) ?? heading ?? '';
const bodyV = (watch('settings.body') as string) ?? body ?? '';
const ctaV = (watch('settings.cta') as CtaValue | null) ?? cta ?? null;
const verticalAlignV =
((watch('settings.verticalAlign') as string) ??
verticalAlign ??
'center') as SplitVerticalAlign;
const imageFitV =
((watch('settings.imageFit') as string) ?? imageFit ?? 'cover') as SplitImageFit;
return (
{
setValue('settings.image', next, { shouldDirty: true });
// Clear stale dimensions when the image is cleared; new
// dimensions arrive via onLoadDimensions on a fresh pick.
if (!next) {
setValue('settings.width', null, { shouldDirty: true });
setValue('settings.height', null, { shouldDirty: true });
}
}}
onLoadDimensions={({ width: w, height: h }) => {
setValue('settings.width', w, { shouldDirty: true });
setValue('settings.height', h, { shouldDirty: true });
}}
/>
setValue('settings.imageAlt', e.target.value, {
shouldDirty: true
})
}
placeholder={_('Describe the image')}
className={drawerInputClass}
/>
value={imageFitV}
options={FIT_OPTIONS}
onChange={(v) =>
setValue('settings.imageFit', v, { shouldDirty: true })
}
/>
value={imagePositionV}
options={POSITION_OPTIONS}
onChange={(v) =>
setValue('settings.imagePosition', v, { shouldDirty: true })
}
/>
value={verticalAlignV}
options={ALIGN_OPTIONS}
onChange={(v) =>
setValue('settings.verticalAlign', v, { shouldDirty: true })
}
/>
{ctaV ? (
<>
setValue('settings.cta', v, { shouldDirty: true })
}
/>
>
) : (
)}
);
}
export const query = `
query Query(
$image: String
$imageAlt: String
$imagePosition: String
$width: Float
$height: Float
$eyebrow: String
$heading: String
$body: String
$cta: JSON
$verticalAlign: String
$imageFit: String
) {
splitFeatureWidget(
image: $image
imageAlt: $imageAlt
imagePosition: $imagePosition
width: $width
height: $height
eyebrow: $eyebrow
heading: $heading
body: $body
cta: $cta
verticalAlign: $verticalAlign
imageFit: $imageFit
) {
image
imageAlt
imagePosition
width
height
eyebrow
heading
body
cta
verticalAlign
imageFit
}
}
`;
export const variables = `{
image: getWidgetSetting("image"),
imageAlt: getWidgetSetting("imageAlt"),
imagePosition: getWidgetSetting("imagePosition", "left"),
width: getWidgetSetting("width"),
height: getWidgetSetting("height"),
eyebrow: getWidgetSetting("eyebrow"),
heading: getWidgetSetting("heading"),
body: getWidgetSetting("body"),
cta: getWidgetSetting("cta"),
verticalAlign: getWidgetSetting("verticalAlign", "center"),
imageFit: getWidgetSetting("imageFit", "cover")
}`;