| 1 |
import { Tooltip } from '@wordpress/components'; |
| 2 |
import { useMemo, useState } from '@wordpress/element'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
import { ErrorMessage } from '@agent/components/ErrorMessage'; |
| 5 |
import { useFontVariationOverride } from '@agent/hooks/useFontVariationOverride'; |
| 6 |
import { useThemeFontsVariations } from '@agent/hooks/useThemeFontsVariations'; |
| 7 |
|
| 8 |
export const SelectThemeFontsVariation = ({ onConfirm, onCancel }) => { |
| 9 |
const [css, setCss] = useState(''); |
| 10 |
const [selected, setSelected] = useState(null); |
| 11 |
const { undoChange } = useFontVariationOverride({ css }); |
| 12 |
const { variations, isLoading } = useThemeFontsVariations(); |
| 13 |
const noVariations = !variations || variations.length === 0; |
| 14 |
const shuffled = useMemo( |
| 15 |
() => (variations ? variations.sort(() => Math.random() - 0.5) : []), |
| 16 |
[variations], |
| 17 |
); |
| 18 |
|
| 19 |
const handleConfirm = () => { |
| 20 |
if (!selected) return; |
| 21 |
onConfirm({ |
| 22 |
data: { variation: variations.find((v) => v.title === selected) }, |
| 23 |
}); |
| 24 |
}; |
| 25 |
|
| 26 |
const handleCancel = () => { |
| 27 |
undoChange(); |
| 28 |
onCancel(); |
| 29 |
}; |
| 30 |
|
| 31 |
if (isLoading) { |
| 32 |
return ( |
| 33 |
<div className="min-h-24 p-2 text-center text-sm"> |
| 34 |
{__('Loading font variations...', 'extendify-local')} |
| 35 |
</div> |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
if (noVariations) { |
| 40 |
return ( |
| 41 |
<ErrorMessage> |
| 42 |
<div className="font-semibold"> |
| 43 |
{__('No font variations found', 'extendify-local')} |
| 44 |
</div> |
| 45 |
<div className=""> |
| 46 |
{__( |
| 47 |
'We were unable to find any variations for your theme.', |
| 48 |
'extendify-local', |
| 49 |
)} |
| 50 |
</div> |
| 51 |
</ErrorMessage> |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
return ( |
| 56 |
<div className="mb-4 ml-10 mr-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50 rtl:ml-2 rtl:mr-10"> |
| 57 |
<div className="rounded-lg border-b border-gray-300 bg-white"> |
| 58 |
<div className="grid grid-cols-2 gap-2 p-3"> |
| 59 |
{shuffled?.slice(0, 10)?.map(({ title, css, styles }) => ( |
| 60 |
<Tooltip key={title} text={title} placement="top"> |
| 61 |
<button |
| 62 |
aria-selected={selected === title} |
| 63 |
type="button" |
| 64 |
style={{ fontFamily: getFont(styles)?.normal }} |
| 65 |
className={`relative flex w-full items-center justify-center overflow-hidden rounded-lg border border-gray-300 bg-none p-2 text-center text-sm ${ |
| 66 |
selected === title ? 'ring-wp ring-design-main' : '' |
| 67 |
}`} |
| 68 |
onClick={() => { |
| 69 |
setSelected(title); |
| 70 |
setCss(css); |
| 71 |
}}> |
| 72 |
<div className="max-w-fit content-stretch items-center justify-center rounded-lg text-2xl rtl:space-x-reverse"> |
| 73 |
<span style={{ fontFamily: getFont(styles)?.heading }}> |
| 74 |
A |
| 75 |
</span> |
| 76 |
<span style={{ fontFamily: getFont(styles)?.normal }}>a</span> |
| 77 |
</div> |
| 78 |
</button> |
| 79 |
</Tooltip> |
| 80 |
))} |
| 81 |
</div> |
| 82 |
</div> |
| 83 |
<div className="flex justify-start gap-2 p-3"> |
| 84 |
<button |
| 85 |
type="button" |
| 86 |
className="w-full rounded border border-gray-300 bg-white p-2 text-sm text-gray-700" |
| 87 |
onClick={handleCancel}> |
| 88 |
{__('Cancel', 'extendify-local')} |
| 89 |
</button> |
| 90 |
<button |
| 91 |
type="button" |
| 92 |
className="w-full rounded border border-design-main bg-design-main p-2 text-sm text-white" |
| 93 |
disabled={!selected} |
| 94 |
onClick={handleConfirm}> |
| 95 |
{__('Save', 'extendify-local')} |
| 96 |
</button> |
| 97 |
</div> |
| 98 |
</div> |
| 99 |
); |
| 100 |
}; |
| 101 |
|
| 102 |
const getFont = (styles) => { |
| 103 |
if ( |
| 104 |
styles?.typography?.fontFamily && |
| 105 |
styles?.elements?.heading?.typography?.fontFamily |
| 106 |
) { |
| 107 |
return { |
| 108 |
normal: styles.typography.fontFamily, |
| 109 |
heading: |
| 110 |
styles.elements.heading.typography.fontFamily || |
| 111 |
styles.typography.fontFamily, |
| 112 |
}; |
| 113 |
} |
| 114 |
|
| 115 |
if (styles?.typography?.fontFamily) { |
| 116 |
return { |
| 117 |
normal: styles.typography.fontFamily, |
| 118 |
heading: styles.typography.fontFamily, |
| 119 |
}; |
| 120 |
} |
| 121 |
}; |
| 122 |
|