UNPKG

1.11 kBJavaScriptView Raw
1import global from 'global';
2import { addons } from '@storybook/addons';
3import { STORY_CHANGED } from '@storybook/core-events';
4import { EVENTS, HIGHLIGHT_STYLE_ID } from './constants';
5import { highlightStyle } from './highlight';
6const {
7 document
8} = global;
9
10if (module && module.hot && module.hot.decline) {
11 module.hot.decline();
12}
13
14const channel = addons.getChannel();
15
16const highlight = infos => {
17 const id = HIGHLIGHT_STYLE_ID;
18 resetHighlight(); // Make sure there are no duplicated selectors
19
20 const elements = Array.from(new Set(infos.elements));
21 const sheet = document.createElement('style');
22 sheet.setAttribute('id', id);
23 sheet.innerHTML = elements.map(target => `${target}{
24 ${highlightStyle(infos.color)}
25 }`).join(' ');
26 document.head.appendChild(sheet);
27};
28
29const resetHighlight = () => {
30 const id = HIGHLIGHT_STYLE_ID;
31 const sheetToBeRemoved = document.getElementById(id);
32
33 if (sheetToBeRemoved) {
34 sheetToBeRemoved.parentNode.removeChild(sheetToBeRemoved);
35 }
36};
37
38channel.on(STORY_CHANGED, resetHighlight);
39channel.on(EVENTS.HIGHLIGHT, highlight);
\No newline at end of file