0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/apps/signup-form/src/utils/options.tsx
Daniel Lockyer 2173fd9ef9 Moved signup-form to apps/ folder
refs https://github.com/TryGhost/Toolbox/issues/594

- we're moving all the standalone apps to a separate folder to keep them
  out of the core code and easier to find
2023-06-19 09:43:33 +02:00

45 lines
1.5 KiB
TypeScript

import React from 'react';
import {SignupFormOptions} from '../AppContext';
export function useOptions(scriptTag: HTMLElement) {
const buildOptions = React.useCallback(() => {
const labels = [];
while (scriptTag.dataset[`label-${labels.length + 1}`]) {
labels.push(scriptTag.dataset[`label-${labels.length + 1}`] as string);
}
return {
title: scriptTag.dataset.title || undefined,
description: scriptTag.dataset.description || undefined,
icon: scriptTag.dataset.icon || undefined,
backgroundColor: scriptTag.dataset.backgroundColor || undefined,
textColor: scriptTag.dataset.textColor || undefined,
buttonColor: scriptTag.dataset.buttonColor || undefined,
buttonTextColor: scriptTag.dataset.buttonTextColor || undefined,
site: scriptTag.dataset.site || window.location.origin,
labels,
locale: scriptTag.dataset.locale || 'en'
};
}, [scriptTag]);
const [options, setOptions] = React.useState<SignupFormOptions>(buildOptions());
React.useEffect(() => {
const observer = new MutationObserver((mutationList) => {
if (mutationList.some(mutation => mutation.type === 'attributes')) {
setOptions(buildOptions());
}
});
observer.observe(scriptTag, {
attributes: true
});
return () => {
observer.disconnect();
};
}, [scriptTag, buildOptions]);
return options;
}