1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00
metamask-extension/.storybook/i18n-party-addon/register.js
George Marshall d72f7295a3
Docs/12367 Adding storybook essentials addons (#12393)
* Adding storybook essentials and documentation contribution guidelines

* Deprecation updates

* Update ui/2.DOCUMENTATION.stories.mdx

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>

* Updating spelling and adding label to i18n-party plugin in toolbar

Co-authored-by: kumavis <kumavis@users.noreply.github.com>
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
2021-10-29 07:22:07 -10:00

42 lines
1.5 KiB
JavaScript

// import { useGlobals } from '@storybook/api';
const { useGlobals } = require('@storybook/api');
const React = require('react');
const { addons, types } = require('@storybook/addons');
const { Icons, IconButton } = require('@storybook/components');
const localeList = require('../../app/_locales/index.json');
const { useEffect } = React;
addons.register('i18n-party', () => {
addons.add('i18n-party', {
title: 'rotates through every i18n locale',
//👇 Sets the type of UI element in Storybook
type: types.TOOL,
match: () => true,
render: (...args) => {
// https://github.com/storybookjs/storybook/blob/6490a0d646dbaa293b76bbde477daca615efe789/addons/toolbars/src/components/MenuToolbar.tsx#L2
const [globals, updateGlobals] = useGlobals();
useEffect(() => {
if (!globals.localeParty) return;
const interval = setInterval((...args) => {
const currentIndex = localeList.findIndex(
({ code }) => code === globals.locale,
);
const nextIndex = (currentIndex + 1) % localeList.length;
const nextLocale = localeList[nextIndex].code;
updateGlobals({ locale: nextLocale });
}, 2000);
return () => clearInterval(interval);
});
return (
<IconButton
onClick={() => updateGlobals({ localeParty: !globals.localeParty })}
>
<Icons icon={globals.localeParty ? 'star' : 'starhollow'} />
<span>&nbsp;Shuffle i18n locale</span>
</IconButton>
);
},
});
});