mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
2173fd9ef9
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
45 lines
1.5 KiB
TypeScript
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;
|
|
}
|