@storybook/addon-links
Version:
Storybook Links: Link stories together to build demos and prototypes with your UI components
66 lines (63 loc) • 1.91 kB
JavaScript
import {
hrefTo,
navigate
} from "../_browser-chunks/chunk-5NYAPMFO.js";
import {
__name
} from "../_browser-chunks/chunk-D24LJQWS.js";
// src/react/components/link.tsx
import React, { PureComponent } from "react";
var LEFT_BUTTON = 0;
var isPlainLeftClick = /* @__PURE__ */ __name((e) => e.button === LEFT_BUTTON && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey, "isPlainLeftClick");
var cancelled = /* @__PURE__ */ __name((e, cb = (_e) => {
}) => {
if (isPlainLeftClick(e)) {
e.preventDefault();
cb(e);
}
}, "cancelled");
var _LinkTo = class _LinkTo extends PureComponent {
constructor() {
super(...arguments);
this.state = {
href: "/"
};
this.updateHref = /* @__PURE__ */ __name(async () => {
const { kind, title = kind, story, name = story } = this.props;
if (title && name) {
const href = await hrefTo(title, name);
this.setState({ href });
}
}, "updateHref");
this.handleClick = /* @__PURE__ */ __name(() => {
const { kind, title = kind, story, name = story } = this.props;
if (title && name) {
navigate({ title, name });
}
}, "handleClick");
}
componentDidMount() {
this.updateHref();
}
componentDidUpdate(prevProps) {
const { kind, title, story, name } = this.props;
if (prevProps.kind !== kind || prevProps.story !== story || prevProps.title !== title || prevProps.name !== name) {
this.updateHref();
}
}
render() {
const { kind, title = kind, story, name = story, children, ...rest } = this.props;
const { href } = this.state;
return React.createElement("a", { href, onClick: (e) => cancelled(e, this.handleClick), ...rest }, children);
}
};
__name(_LinkTo, "LinkTo");
_LinkTo.defaultProps = {
children: void 0
};
var LinkTo = _LinkTo;
// src/react/index.ts
var react_default = LinkTo;
export {
react_default as default
};