import React, { useState } from 'react'; import { Meta, StoryObj } from '@storybook/react-webpack5'; import { createSandboxStory, globalScope, } from '../../../.storybook/components/sandbox/SandboxEditor'; import { storySourceWithoutNoise } from '../../../.storybook/helpers'; import { Markup } from '../Markup'; export default { component: Markup, title: 'Typography/Markup', tags: ['early-access'], } satisfies Meta; type Story = StoryObj; const MARKUP_STRING = `This markup string supports number of tags: important, positive, negative, strong, strikethrough with a screen-reader-friendly label, and a link. It respects line breaks and also allows for paragraphs and nest tags. `; export const Playground: Story = { args: { children: MARKUP_STRING, }, }; export const Sandbox = createSandboxStory({ code: ` {\`${MARKUP_STRING}\`} `, scope: globalScope, }); /** * Renders the design system `Link` component inline. `` is an alias for ``. External * links with `target="_blank"` automatically receive a navigate-away icon and `rel="noreferrer"`. * */ export const WithLinks: Story = { args: { children: 'Using "link" tag: settings. Using "a" tag: our website.', }, }; /** * Button-mode link using the `action` attribute. The handler is provided via the `onAction` prop. * If `onAction` is not provided, the link degrades to plain text. * */ export const WithLinksAsButtons = storySourceWithoutNoise({ args: { children: 'Click increment to add `1`.', }, render: function Render(args) { const [count, setCount] = useState(0); const handleAction = (actionId: string) => { if (actionId === 'increment') { setCount((prev) => prev + 1); } }; return ( <> {args.children} {count} ); }, }); /** * `` renders a `` element. `` renders a `` element for * struck-through text. * */ export const StrongAndStrikethrough: Story = { args: { children: 'This is bold and this is no longer relevant.', }, }; /** * Block-level paragraphs. `

` is an alias for ``. Both render a `

` element. * */ export const Paragraphs: Story = { args: { children: 'First paragraph using the full tag name.

Second paragraph using the short alias.

', }, }; /** * Tags can nest to arbitrary depth — emphasis wrapping a link, strong inside a paragraph, etc. * */ export const Nested: Story = { args: { children: '

Read the terms and conditions carefully

', }, }; /** * Literal newline characters (`\n`) in the string are converted to `
` elements. * */ export const Newlines: Story = { args: { children: 'First line.\nSecond line.\nThird line.', }, }; /** * Non-string children (JSX, components, numbers) pass through unchanged — no parsing occurs. * This lets parent components use Markup as a unified slot for both markup strings and JSX. * */ export const PassThrough: Story = { render: () => ( This JSX is passed through {' '} without parsing. ), }; /** * The wrapper element is automatically chosen based on content: `` for inline-only markup, * `
` when `` nodes are present (to avoid invalid `

` inside ``). * The `as` prop overrides auto-detection when you need explicit control. * */ export const WrapperElement: Story = { parameters: { docs: { source: { type: 'dynamic' }, }, }, render: () => ( <> {'This is inline content — wrapper is a span.'} {'

Block content with a paragraph.

Second paragraph.

'} {'

Paragraph forced into a section wrapper via the as prop.

'}
), }; /** * Not all semantic tags are announced by screen readers — for example, `` (strikethrough) is * often skipped. The `accessibilityLabel` attribute provides extended copy for visually impaired * users, ensuring the meaning is conveyed even when the visual styling is not perceived. It is * rendered as `aria-label` on the DOM element. * */ export const AccessibilityLabels: Story = { args: { children: [ 'Was £50.00,', 'now £35.00.', 'Limited time offer', ].join(' '), }, }; /** * Use the `data` prop whenever the markup template contains values that originate from * user-generated content or external API responses (e.g. recipient names, amounts, dates). * This separates trusted template structure from untrusted data content, preventing markup * injection attacks. * * All matched Mustache-style placeholders (`{{key}}`) are replaced (including duplicates) with * values from the `data` object. If a key is not found, it will be rendered literally as `{{key}}`. * Data values are always rendered as plain text — even if they contain markup tags, those are * displayed literally and never parsed into elements. * */ export const DataSubstitution: Story = { parameters: { docs: { source: { type: 'dynamic' }, }, }, render: () => ( <> {'You sent {{amount}} to {{recipient}}. {{amount}} is now pending.'} Click here' }}> {'Hello, {{name}}!'} ), }; /** * Controls which link destinations are allowed. By default, only root-relative paths * and `wise.com` URLs render as links. External domains are blocked. * * - `"internal"` (default) — `/*` and `https://wise.com/*` only * - `"all"` — any href that passes base sanitisation * - `"none"` — all href-based links render as plain text * * The prop is an additional restriction — it never bypasses protocol/host sanitisation. * */ export const AllowLinks: Story = { parameters: { docs: { source: { type: 'dynamic' }, }, }, render: () => ( <> { 'By default only internal or wise.com links are allowed, while all external ones are blocked.' } { 'If set to `all`, every internal, wise.com and external link will be respected.' } { 'If set to `none `, every internal, wise.com and external link will render as plain text.' } ), }; /** * The component applies a strict whitelist of allowed tags and attributes. Malicious or malformed input is rendered as literal text, preventing XSS attacks. [Full list of handled attack vectors](?path=/docs/typography-markup-parser-security--docs) * */ export const MalformedInput: Story = { args: { children: [ '', '', '', 'blocked protocol', 'data URI', 'vbscript', 'ignored attribute', 'this is safe', ].join('\n'), }, };