ar-test/app/components/projects/form/logo-background-field.tsx

39 lines
1.7 KiB
TypeScript

import { useFormContext, useWatch } from 'react-hook-form';
import { ColorPicker } from '~/components/ui/color-picker';
import { Field } from '~/components/ui/field';
import { LOGO_BACKGROUND_PRESETS } from '~/data/projects/constants';
import { isDarkColor } from '~/lib/color';
import type { ProjectFormValues } from '~/schemas/projects/project-form';
import type { LogoBackground } from '~/types/project';
const PRESET_KEYS = Object.keys(LOGO_BACKGROUND_PRESETS) as LogoBackground[];
const PRESETS = PRESET_KEYS.map((key) => LOGO_BACKGROUND_PRESETS[key]);
// Light / Dark presets or any custom colour; a custom colour also decides light vs dark for contrast
export function LogoBackgroundField() {
const { control, setValue } = useFormContext<ProjectFormValues>();
const [logoBg, logoBgColor] = useWatch({ control, name: ['logoBg', 'logoBgColor'] });
const onChange = (hex: string) => {
const preset = PRESET_KEYS.find((key) => LOGO_BACKGROUND_PRESETS[key].value === hex);
setValue('logoBg', preset ?? (isDarkColor(hex) ? 'dark' : 'light'), { shouldDirty: true });
setValue('logoBgColor', preset ? '' : hex, { shouldDirty: true });
};
return (
<Field
label='Logo background'
htmlFor='project-logo-bg'
hint='Backing behind the logo on the light AR page. Pick a dark colour for white or light logos.'
>
<ColorPicker
id='project-logo-bg'
label='Logo background'
value={logoBgColor || LOGO_BACKGROUND_PRESETS[logoBg].value}
presets={PRESETS}
onChange={onChange}
/>
</Field>
);
}