import React from 'react';
import { useAddonState } from '@storybook/api';
import { addons, types } from '@storybook/addons';
import { IconButton } from '@storybook/components';
const ADDON_ID = 'addon-rtl';
const getIframe = (() => {
let cached: HTMLIFrameElement | null = null;
return () => {
if (cached) return cached;
cached = document.getElementById('storybook-preview-iframe') as HTMLIFrameElement;
return cached;
};
})();
const RTLButton = () => {
const [rtl, setRTL] = useAddonState(ADDON_ID, false);
const handleClick = () => {
const previewEl = getIframe();
const wrapperEl = previewEl.contentDocument!.body;
const nextRTL = wrapperEl.getAttribute('dir') !== 'rtl';
setRTL(nextRTL);
wrapperEl.setAttribute('dir', nextRTL ? 'rtl' : 'ltr');
};
return (
);
};
addons.register(ADDON_ID, (/* api */) => {
addons.add('addon-rtl/tool', {
title: '',
type: types.TOOL,
match: ({ viewMode }) => viewMode === 'story',
render: () => ,
});
});