Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/html-preview-segmented-control.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/stand': patch
---

HtmlPreview / HtmlPreviewLoader: switch preview widths with a `SegmentedControl` (plain number labels) instead of a `RadioGroup`. The control now sits below the title, aligned with the left edge of the centred preview, and moves with it. The layout also follows the updated design: a 1px border, 16px padding and gap on the container, a Heading 2xl string title, and a transparent preview frame. New `borderColor`, `borderStyle`, `borderWidth` and `gap` theme tokens were added.
95 changes: 46 additions & 49 deletions src/components/HtmlPreviewLoader/HtmlPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { css } from '@emotion/react';
import { useState } from 'react';
import { mergeDeep } from '../../util/mergeDeep';
import { AlertBanner } from '../AlertBanner/AlertBanner';
import { Button } from '../Button/Button';
import { Icon } from '../Icon/Icon';
import { Radio, RadioGroup } from '../RadioGroup/RadioGroup';
import {
SegmentedControl,
SegmentedControlItem,
} from '../SegmentedControl/SegmentedControl';
import { Typography } from '../Typography/Typography';
import {
alertBannerContentsStyle,
centeredStyle,
defaultHtmlPreviewLoaderTheme,
headerStyles,
htmlPreviewLoaderStyles,
iframeStyle,
loadingIconStyle,
previewColumnStyle,
previewFrameStyle,
reloadButtonStyle,
spinAnimationStyle,
Expand Down Expand Up @@ -43,39 +45,15 @@ export const HtmlPreview = ({

return (
<div css={[htmlPreviewLoaderStyles(mergedTheme), cssOverrides]}>
<header css={headerStyles()}>
{typeof title === 'string' ? (
<Typography variant="titleMd">{title}</Typography>
) : (
title
)}
{widthOptions.length > 0 && (
<RadioGroup
size="sm"
label="width"
orientation="horizontal"
formInputContainerTheme={{
shared: {
container: {
width: 'unset',
flexDirection: 'row',
},
},
}}
cssOverrides={css({
alignItems: 'center',
})}
value={String(frameWidth)}
onChange={(value) => setFrameWidth(Number(value))}
>
{widthOptions.map((width, index) => (
<Radio key={index} value={String(width)}>
{width}px
</Radio>
))}
</RadioGroup>
)}
</header>
{title ? (
<header>
{typeof title === 'string' ? (
<Typography variant="heading2Xl">{title}</Typography>
) : (
title
)}
</header>
) : null}

<div
css={[
Expand All @@ -85,19 +63,38 @@ export const HtmlPreview = ({
},
]}
>
{html && (
<iframe
sandbox=""
style={{
minWidth: frameWidth,
maxWidth: frameWidth,
background: frameBackground,
}}
css={iframeStyle(isLoading || !!errorMessage)}
title="preview"
srcDoc={html}
/>
)}
<div
css={previewColumnStyle(mergedTheme)}
style={{ width: frameWidth }}
>
{widthOptions.length > 0 && (
<SegmentedControl
aria-label="Preview width"
selectedKey={frameWidth}
onSelectionChange={(key) => setFrameWidth(Number(key))}
>
{widthOptions.map((width) => (
<SegmentedControlItem key={width} id={width}>
{width}
</SegmentedControlItem>
))}
</SegmentedControl>
)}

{html && (
<iframe
sandbox=""
style={{
minWidth: frameWidth,
maxWidth: frameWidth,
background: frameBackground,
}}
css={iframeStyle(isLoading || !!errorMessage)}
title="preview"
srcDoc={html}
/>
)}
</div>

{showReloadButton && (
<div css={reloadButtonStyle(mergedTheme)}>
Expand Down
6 changes: 3 additions & 3 deletions src/components/HtmlPreviewLoader/HtmlPreviewLoader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The HtmlPreviewLoader component fetches HTML markup, renders it inside an iframe

- When you need to preview HTML content such as email templates or document markup in the UI.
- When the preview content is loaded asynchronously and should show loading or error feedback.
- When the preview should support width switching and an optional reload affordance.
- When the preview should support width switching (via a segmented control above the preview) and an optional reload affordance.

## Peer dependencies

Expand Down Expand Up @@ -77,9 +77,9 @@ export const Component = () => (
| Name | Type | Required | Default | Description |
| ----------------- | ----------------------------- | -------- | ---------------------- | ------------------------------------------------------------------------- |
| `fetchHtml` | `{ (): Promise<string> }` | Yes | N/A | Function that resolves to the HTML string to render in the preview frame. |
| `title` | `ReactNode` | Yes | N/A | Title content shown above the preview. |
| `title` | `ReactNode` | Yes | N/A | Title content shown above the preview. Pass markup to include a subtitle. |
| `minHeight` | `number` | No | `600` | Minimum height of the preview frame in pixels. |
| `widthOptions` | `number[]` | No | `[320, 375, 425, 650]` | Available frame widths shown in the width selector. |
| `widthOptions` | `number[]` | No | `[320, 375, 425, 650]` | Frame widths offered in the segmented control. Pass `[]` to hide it. |
| `defaultWidth` | `number` | No | `425` | Initial width used for the preview frame. |
| `frameBackground` | `CSSProperties['background']` | No | `'white'` | Background applied to the iframe. |
| `allowReloading` | `boolean` | No | `false` | Shows a reload button when the preview can be refreshed. |
Expand Down
12 changes: 12 additions & 0 deletions src/components/HtmlPreviewLoader/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const componentCss = /* css */ `@import '@guardian/stand/component/htmlPr
padding-right: var(--component-html-preview-loader-padding-right);
padding-bottom: var(--component-html-preview-loader-padding-bottom);
padding-left: var(--component-html-preview-loader-padding-left);
border-width: var(--component-html-preview-loader-border-width);
border-style: var(--component-html-preview-loader-border-style);
border-color: var(--component-html-preview-loader-border-color);
display: flex;
flex-direction: column;
gap: var(--component-html-preview-loader-gap);
background: var(--component-html-preview-loader-shared-color-background);
color: var(--component-html-preview-loader-shared-color-text);
}
Expand Down Expand Up @@ -65,6 +71,12 @@ const styles = \`
padding-right: \${componentHtmlPreviewLoader.paddingRight};
padding-bottom: \${componentHtmlPreviewLoader.paddingBottom};
padding-left: \${componentHtmlPreviewLoader.paddingLeft};
border-width: \${componentHtmlPreviewLoader.borderWidth};
border-style: \${componentHtmlPreviewLoader.borderStyle};
border-color: \${componentHtmlPreviewLoader.borderColor};
display: flex;
flex-direction: column;
gap: \${componentHtmlPreviewLoader.gap};
background: \${componentHtmlPreviewLoader.shared.color.background};
color: \${componentHtmlPreviewLoader.shared.color.text};
\`;
Expand Down
19 changes: 15 additions & 4 deletions src/components/HtmlPreviewLoader/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ export const htmlPreviewLoaderStyles = (
padding-bottom: ${theme.paddingBottom};
padding-left: ${theme.paddingLeft};
padding-right: ${theme.paddingRight};
border-color: ${theme.borderColor};
border-style: ${theme.borderStyle};
border-width: ${theme.borderWidth};
display: flex;
flex-direction: column;
gap: ${theme.gap};
position: relative;
`;
};

export const headerStyles = (): SerializedStyles => {
export const previewColumnStyle = ({
gap,
}: HtmlPreviewLoaderTheme): SerializedStyles => {
return css`
display: flex;
align-items: center;
flex-wrap: wrap;
justify-content: space-between;
flex-direction: column;
align-items: flex-start;
flex-shrink: 0;
gap: ${gap};
`;
};

Expand All @@ -58,6 +67,8 @@ export const previewFrameStyle = ({
export const iframeStyle = (isNotLoaded: boolean): SerializedStyles => {
return css`
border: none;
flex: 1;
align-self: stretch;
transition: 'filter 0.25s';
${
isNotLoaded
Expand Down
21 changes: 15 additions & 6 deletions src/styleD/build/css/component/htmlPreviewLoader.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
*/

:root {
--component-html-preview-loader-border-color: #cccccc;
--component-html-preview-loader-border-style: solid;
--component-html-preview-loader-border-width: 0.0625rem;
--component-html-preview-loader-gap: 1rem;
--component-html-preview-loader-loading-icon-color: #8d8d8d;
--component-html-preview-loader-loading-icon-size: 3rem;
--component-html-preview-loader-padding-bottom: 0px;
--component-html-preview-loader-padding-left: 0.5rem;
--component-html-preview-loader-padding-right: 0.5rem;
--component-html-preview-loader-padding-top: 0px;
--component-html-preview-loader-preview-frame-background-color: #ededed;
--component-html-preview-loader-preview-frame-border-color: #545454;
--component-html-preview-loader-padding-bottom: 1rem;
--component-html-preview-loader-padding-left: 1rem;
--component-html-preview-loader-padding-right: 1rem;
--component-html-preview-loader-padding-top: 1rem;
--component-html-preview-loader-preview-frame-background-color: rgba(
0,
0,
0,
0
);
--component-html-preview-loader-preview-frame-border-color: rgba(0, 0, 0, 0);
--component-html-preview-loader-preview-frame-border-style: solid;
--component-html-preview-loader-preview-frame-border-width: 0.0625rem;
--component-html-preview-loader-preview-frame-padding-bottom: 0.5rem;
Expand Down
16 changes: 10 additions & 6 deletions src/styleD/build/typescript/component/htmlPreviewLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
*/

export const componentHtmlPreviewLoader = {
borderColor: '#cccccc',
borderStyle: 'solid',
borderWidth: '0.0625rem',
gap: '1rem',
loadingIcon: {
color: '#8d8d8d',
size: '3rem',
},
paddingBottom: '0px',
paddingLeft: '0.5rem',
paddingRight: '0.5rem',
paddingTop: '0px',
paddingBottom: '1rem',
paddingLeft: '1rem',
paddingRight: '1rem',
paddingTop: '1rem',
previewFrame: {
backgroundColor: '#ededed',
borderColor: '#545454',
backgroundColor: '#00000000',
borderColor: '#00000000',
borderStyle: 'solid',
borderWidth: '0.0625rem',
paddingBottom: '0.5rem',
Expand Down
34 changes: 22 additions & 12 deletions src/styleD/tokens/component/htmlPreviewLoader.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
{
"component": {
"htmlPreviewLoader": {
"borderColor": {
"$type": "color",
"$value": "{semantic.colors.border.weak}"
},
"borderStyle": {
"$type": "strokeStyle",
"$value": "solid"
},
"borderWidth": {
"$type": "dimension",
"$value": "{semantic.sizing.border.default}"
},
"gap": {
"$type": "dimension",
"$value": "{semantic.spacing.stackMd}"
},
"loadingIcon": {
"color": {
"$type": "color",
Expand All @@ -13,34 +29,28 @@
},
"paddingBottom": {
"$type": "dimension",
"$value": {
"unit": "px",
"value": 0
}
"$value": "{semantic.spacing.stackMd}"
},
"paddingLeft": {
"$type": "dimension",
"$value": "{base.spacing.8Rem}"
"$value": "{semantic.spacing.stackMd}"
},
"paddingRight": {
"$type": "dimension",
"$value": "{base.spacing.8Rem}"
"$value": "{semantic.spacing.stackMd}"
},
"paddingTop": {
"$type": "dimension",
"$value": {
"unit": "px",
"value": 0
}
"$value": "{semantic.spacing.stackMd}"
},
"previewFrame": {
"backgroundColor": {
"$type": "color",
"$value": "{semantic.colors.fill.neutralWeak}"
"$value": "{base.colors.transparent}"
},
"borderColor": {
"$type": "color",
"$value": "{semantic.colors.border.stronger}"
"$value": "{base.colors.transparent}"
},
"borderStyle": {
"$type": "strokeStyle",
Expand Down
Loading