@storybook/addon-links 5.2.1 → 5.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/addon-links",
3
- "version": "5.2.1",
3
+ "version": "5.2.5",
4
4
  "description": "Story Links addon for storybook",
5
5
  "keywords": [
6
6
  "addon",
@@ -16,15 +16,22 @@
16
16
  "directory": "addons/links"
17
17
  },
18
18
  "license": "MIT",
19
+ "files": [
20
+ "dist/**/*",
21
+ "docs/**/*",
22
+ "README.md",
23
+ "*.js",
24
+ "*.d.ts"
25
+ ],
19
26
  "main": "dist/index.js",
20
27
  "types": "dist/index.d.ts",
21
28
  "scripts": {
22
29
  "prepare": "node ../../scripts/prepare.js"
23
30
  },
24
31
  "dependencies": {
25
- "@storybook/addons": "5.2.1",
26
- "@storybook/core-events": "5.2.1",
27
- "@storybook/router": "5.2.1",
32
+ "@storybook/addons": "5.2.5",
33
+ "@storybook/core-events": "5.2.5",
34
+ "@storybook/router": "5.2.5",
28
35
  "common-tags": "^1.8.0",
29
36
  "core-js": "^3.0.1",
30
37
  "global": "^4.3.2",
@@ -37,5 +44,5 @@
37
44
  "publishConfig": {
38
45
  "access": "public"
39
46
  },
40
- "gitHead": "5497d2ea8d47c4573d245126e6f3f45bc176c12d"
47
+ "gitHead": "6e80db697f865f833cf3e250573a7ce36e0ee02a"
41
48
  }
package/src/constants.ts DELETED
@@ -1,7 +0,0 @@
1
- export const ADDON_ID = 'storybook/links';
2
-
3
- export default {
4
- NAVIGATE: `${ADDON_ID}/navigate`,
5
- REQUEST: `${ADDON_ID}/request`,
6
- RECEIVE: `${ADDON_ID}/receive`,
7
- };
package/src/index.ts DELETED
@@ -1,22 +0,0 @@
1
- import { stripIndents } from 'common-tags';
2
- import { linkTo, hrefTo, withLinks } from './preview';
3
-
4
- let hasWarned = false;
5
-
6
- export function LinkTo(): null {
7
- if (!hasWarned) {
8
- // eslint-disable-next-line no-console
9
- console.error(stripIndents`
10
- LinkTo has moved to addon-links/react:
11
- import LinkTo from '@storybook/addon-links/react';
12
- `);
13
- hasWarned = true;
14
- }
15
- return null;
16
- }
17
-
18
- export { linkTo, hrefTo, withLinks };
19
-
20
- if (module && module.hot && module.hot.decline) {
21
- module.hot.decline();
22
- }
package/src/manager.ts DELETED
@@ -1,14 +0,0 @@
1
- import addons from '@storybook/addons';
2
-
3
- import EVENTS, { ADDON_ID } from './constants';
4
-
5
- export const register = () => {
6
- addons.register(ADDON_ID, api => {
7
- const channel = addons.getChannel();
8
-
9
- channel.on(EVENTS.REQUEST, ({ kind, name }) => {
10
- const id = api.storyId(kind, name);
11
- api.emit(EVENTS.RECEIVE, id);
12
- });
13
- });
14
- };
@@ -1,53 +0,0 @@
1
- import addons from '@storybook/addons';
2
- import { SELECT_STORY } from '@storybook/core-events';
3
- import { linkTo, hrefTo } from './preview';
4
-
5
- jest.mock('@storybook/addons');
6
-
7
- export const mockChannel = () => {
8
- return {
9
- emit: jest.fn(),
10
- on: jest.fn(),
11
- once: jest.fn(),
12
- };
13
- };
14
-
15
- describe('preview', () => {
16
- describe('linkTo()', () => {
17
- it('should select the kind and story provided', () => {
18
- const channel = { emit: jest.fn() };
19
- addons.getChannel.mockReturnValue(channel);
20
-
21
- const handler = linkTo('kind', 'name');
22
- handler();
23
-
24
- expect(channel.emit).toHaveBeenCalledWith(SELECT_STORY, {
25
- kind: 'kind',
26
- story: 'name',
27
- });
28
- });
29
-
30
- it('should handle functions returning strings', () => {
31
- const channel = { emit: jest.fn() };
32
- addons.getChannel.mockReturnValue(channel);
33
-
34
- const handler = linkTo((a, b) => a + b, (a, b) => b + a);
35
- handler('kind', 'name');
36
-
37
- expect(channel.emit.mock.calls).toContainEqual([
38
- SELECT_STORY,
39
- {
40
- kind: 'kindname',
41
- story: 'namekind',
42
- },
43
- ]);
44
- });
45
- });
46
-
47
- describe('hrefTo()', () => {
48
- it('should return promise resolved with story href', async () => {
49
- const href = await hrefTo('kind', 'name');
50
- expect(href).toContain('?id=kind--name');
51
- });
52
- });
53
- });
package/src/preview.ts DELETED
@@ -1,76 +0,0 @@
1
- import { document, HTMLElement } from 'global';
2
- import qs from 'qs';
3
- import addons from '@storybook/addons';
4
- import { STORY_CHANGED, SELECT_STORY } from '@storybook/core-events';
5
- import { toId } from '@storybook/router/utils';
6
-
7
- interface ParamsId {
8
- storyId: string;
9
- }
10
- interface ParamsCombo {
11
- kind: string;
12
- story: string;
13
- }
14
-
15
- export const navigate = (params: ParamsId | ParamsCombo) =>
16
- addons.getChannel().emit(SELECT_STORY, params);
17
-
18
- const generateUrl = (id: string) => {
19
- const { location } = document;
20
- const query = qs.parse(location.search, { ignoreQueryPrefix: true });
21
- return `${location.origin + location.pathname}?${qs.stringify(
22
- { ...query, id },
23
- { encode: false }
24
- )}`;
25
- };
26
-
27
- const valueOrCall = (args: string[]) => (value: string | ((...args: string[]) => string)) =>
28
- typeof value === 'function' ? value(...args) : value;
29
-
30
- export const linkTo = (kind: string, story?: string) => (...args: string[]) => {
31
- const resolver = valueOrCall(args);
32
-
33
- navigate({
34
- kind: resolver(kind),
35
- story: resolver(story),
36
- });
37
- };
38
-
39
- export const hrefTo = (kind: string, name: string): Promise<string> =>
40
- new Promise(resolve => {
41
- resolve(generateUrl(toId(kind, name)));
42
- });
43
-
44
- const linksListener = (e: Event) => {
45
- const { target } = e;
46
- if (!(target instanceof HTMLElement)) {
47
- return;
48
- }
49
- const element = target as HTMLElement;
50
- const { sbKind: kind, sbStory: story } = element.dataset;
51
- if (kind || story) {
52
- e.preventDefault();
53
- navigate({ kind, story });
54
- }
55
- };
56
-
57
- let hasListener = false;
58
-
59
- const on = () => {
60
- if (!hasListener) {
61
- hasListener = true;
62
- document.addEventListener('click', linksListener);
63
- }
64
- };
65
- const off = () => {
66
- if (hasListener) {
67
- hasListener = false;
68
- document.removeEventListener('click', linksListener);
69
- }
70
- };
71
-
72
- export const withLinks = (storyFn: () => void) => {
73
- on();
74
- addons.getChannel().once(STORY_CHANGED, off);
75
- return storyFn();
76
- };
@@ -1,50 +0,0 @@
1
- import PropTypes from 'prop-types';
2
- import React from 'react';
3
-
4
- // NOTE: this is a copy of `lib/components/src/navigation/RoutedLink.js`.
5
- // It's duplicated here because that copy has an explicit dependency on
6
- // React 16.3+, which breaks older versions of React running in the preview.
7
- // The proper DRY solution is to create a new package that doesn't depend
8
- // on a specific react version. However, that's a heavy-handed solution for
9
- // one trivial file.
10
-
11
- const LEFT_BUTTON = 0;
12
- // Cmd/Ctrl/Shift/Alt + Click should trigger default browser behaviour. Same applies to non-left clicks
13
- const isPlainLeftClick = e =>
14
- e.button === LEFT_BUTTON && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey;
15
-
16
- export default class RoutedLink extends React.Component {
17
- onClick = e => {
18
- const { onClick } = this.props;
19
-
20
- if (isPlainLeftClick(e)) {
21
- e.preventDefault();
22
- onClick(e);
23
- }
24
- };
25
-
26
- render() {
27
- const { href, children, onClick, className, style } = this.props;
28
- const props = onClick
29
- ? { href, className, style, onClick: this.onClick }
30
- : { href, className, style };
31
- return <a {...props}>{children}</a>;
32
- }
33
- }
34
-
35
- RoutedLink.defaultProps = {
36
- onClick: null,
37
- href: '#',
38
- children: null,
39
- className: undefined,
40
- style: undefined,
41
- };
42
-
43
- RoutedLink.propTypes = {
44
- onClick: PropTypes.func,
45
- href: PropTypes.string,
46
- children: PropTypes.node,
47
- className: PropTypes.string,
48
- // eslint-disable-next-line react/forbid-prop-types
49
- style: PropTypes.object,
50
- };
@@ -1,8 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`LinkTo render should render a link 1`] = `
4
- <a
5
- href="http://localhost/?id=foo--bar"
6
- onClick={[Function]}
7
- />
8
- `;
@@ -1,43 +0,0 @@
1
- import { shallow } from 'enzyme';
2
- import React from 'react';
3
- import addons from '@storybook/addons';
4
-
5
- import { SELECT_STORY } from '@storybook/core-events';
6
- import { mockChannel } from '../../preview.test';
7
- import LinkTo from './link';
8
-
9
- jest.mock('@storybook/addons');
10
-
11
- describe('LinkTo', () => {
12
- describe('render', () => {
13
- it('should render a link', async () => {
14
- const channel = mockChannel();
15
- addons.getChannel.mockReturnValue(channel);
16
-
17
- const wrapper = shallow(<LinkTo kind="foo" story="bar" />);
18
- await wrapper.instance().updateHref(wrapper.props());
19
- wrapper.update();
20
- expect(wrapper).toMatchSnapshot();
21
- });
22
- });
23
-
24
- describe('events', () => {
25
- it('should select the kind and story on click', () => {
26
- const channel = {
27
- emit: jest.fn(),
28
- on: jest.fn(),
29
- };
30
- addons.getChannel.mockReturnValue(channel);
31
-
32
- const wrapper = shallow(<LinkTo kind="foo" story="bar" />);
33
- wrapper.simulate('click', { button: 0, preventDefault: () => {} });
34
- expect(channel.emit.mock.calls).toContainEqual([
35
- SELECT_STORY,
36
- {
37
- kind: 'foo',
38
- story: 'bar',
39
- },
40
- ]);
41
- });
42
- });
43
- });
@@ -1,74 +0,0 @@
1
- import React, { PureComponent } from 'react';
2
-
3
- import { navigate, hrefTo } from '../../preview';
4
-
5
- // FIXME: copied from Typography.Link. Code is duplicated to
6
- // avoid emotion dependency which breaks React 15.x back-compat
7
-
8
- // Cmd/Ctrl/Shift/Alt + Click should trigger default browser behaviour. Same applies to non-left clicks
9
- const LEFT_BUTTON = 0;
10
-
11
- const isPlainLeftClick = (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>) =>
12
- e.button === LEFT_BUTTON && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey;
13
-
14
- const cancelled = (e: React.MouseEvent<HTMLAnchorElement, MouseEvent>, cb = (_e: any) => {}) => {
15
- if (isPlainLeftClick(e)) {
16
- e.preventDefault();
17
- cb(e);
18
- }
19
- };
20
-
21
- interface Props {
22
- kind: string;
23
- story: string;
24
- children: React.ReactNode;
25
- }
26
-
27
- interface State {
28
- href: string;
29
- }
30
-
31
- export default class LinkTo extends PureComponent<Props, State> {
32
- static defaultProps: Props = {
33
- kind: null,
34
- story: null,
35
- children: undefined,
36
- };
37
-
38
- state: State = {
39
- href: '/',
40
- };
41
-
42
- componentDidMount() {
43
- this.updateHref();
44
- }
45
-
46
- componentDidUpdate(prevProps: Props) {
47
- const { kind, story } = this.props;
48
-
49
- if (prevProps.kind !== kind || prevProps.story !== story) {
50
- this.updateHref();
51
- }
52
- }
53
-
54
- updateHref = async () => {
55
- const { kind, story } = this.props;
56
- const href = await hrefTo(kind, story);
57
- this.setState({ href });
58
- };
59
-
60
- handleClick = () => {
61
- navigate(this.props);
62
- };
63
-
64
- render() {
65
- const { kind, story, children, ...rest } = this.props;
66
- const { href } = this.state;
67
-
68
- return (
69
- <a href={href} onClick={e => cancelled(e, this.handleClick)} {...rest}>
70
- {children}
71
- </a>
72
- );
73
- }
74
- }
@@ -1,3 +0,0 @@
1
- import LinkTo from './components/link';
2
-
3
- export default LinkTo;
package/src/typings.d.ts DELETED
@@ -1 +0,0 @@
1
- declare module 'global';
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "types": ["webpack-env"]
6
- },
7
- "include": [
8
- "src/**/*"
9
- ],
10
- "exclude": [
11
- "src/__tests__/**/*"
12
- ]
13
- }