Skip to content

Commit

Permalink
chore(storybook): add support for playgrounds
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Mar 22, 2019
1 parent 5225617 commit 256e4e4
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 186 deletions.
186 changes: 0 additions & 186 deletions .storybook/decorators.js

This file was deleted.

98 changes: 98 additions & 0 deletions .storybook/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { action } from '@storybook/addon-actions';
import algoliasearch from 'algoliasearch/lite';
import instantsearch from '../src/index';
import moviesPlayground from './playgrounds/movies';
import defaultPlayground from './playgrounds/default';

export const withHits = (
storyFn: ({
container,
instantsearch,
search,
}: {
container: HTMLElement;
instantsearch: any;
search: any;
}) => void,
searchOptions: any
) => () => {
const {
appId = 'latency',
apiKey = '6be0576ff61c053d5f9a3225e2a90f76',
indexName = 'instant_search',
searchParameters = {},
playground = 'default',
...instantsearchOptions
} = searchOptions || {};

const urlLogger = action('Routing state');
const search = instantsearch({
indexName,
searchClient: algoliasearch(appId, apiKey),
searchParameters: {
hitsPerPage: 4,
attributesToSnippet: ['description:15'],
snippetEllipsisText: '[…]',
...searchParameters,
},
routing: {
router: {
write: (routeState: object) => {
urlLogger(JSON.stringify(routeState, null, 2));
},
read: () => ({}),
createURL: () => '',
onUpdate: () => {},
},
},
...instantsearchOptions,
});

const containerElement = document.createElement('div');

// Add the preview container to add the stories in
const previewElement = document.createElement('div');
previewElement.classList.add('container', 'container-preview');
containerElement.appendChild(previewElement);

// Add the playground container to add widgets into
const playgroundElement = document.createElement('div');
playgroundElement.classList.add('container', 'container-playground');
containerElement.appendChild(playgroundElement);

const leftPanelPlaygroundElement = document.createElement('div');
leftPanelPlaygroundElement.classList.add('panel-left');
playgroundElement.appendChild(leftPanelPlaygroundElement);

const rightPanelPlaygroundElement = document.createElement('div');
rightPanelPlaygroundElement.classList.add('panel-right');
playgroundElement.appendChild(rightPanelPlaygroundElement);

switch (playground) {
case 'movies': {
moviesPlayground({
search,
leftPanel: leftPanelPlaygroundElement,
rightPanel: rightPanelPlaygroundElement,
});
break;
}
default: {
defaultPlayground({
search,
leftPanel: leftPanelPlaygroundElement,
rightPanel: rightPanelPlaygroundElement,
});
}
}

storyFn({
container: previewElement,
instantsearch,
search,
});

search.start();

return containerElement;
};
Loading

0 comments on commit 256e4e4

Please sign in to comment.