Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply title from url on page load #360

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
8 changes: 8 additions & 0 deletions .changeset/green-plants-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'playroom': patch
---

Apply `title` from url on page load

Previously the document `title` would only update when the frames panel is open.
The title is now correctly reflected from the url on page load.
18 changes: 17 additions & 1 deletion cypress/e2e/urlHandling.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe('URL handling', () => {

assertFramesMatch([375, 768]);
});

it('title', () => {
cy.visit(
'http://localhost:9000/#?code=N4Ig7glgJgLgFgZxALgNoF0A0IYRgGwFMUQAVQhGEAXyA'
);

cy.title().should('eq', 'Test | Playroom');
});
});

describe('where paramType is search', () => {
Expand All @@ -34,7 +42,7 @@ describe('URL handling', () => {
assertCodePaneContains('<Foo><Foo><Bar/></Foo></Foo>');
});

it('widths', () => {
it('widths and themes', () => {
cy.visit(
'http://localhost:9001/?code=N4Ig7glgJgLgFgZxALgNoGYDsBWANJgNgA4BdAXyA'
);
Expand All @@ -46,5 +54,13 @@ describe('URL handling', () => {
['themeTwo', 768],
]);
});

it('title', () => {
cy.visit(
'http://localhost:9001/?code=N4Ig7glgJgLgFgZxALgNoF0A0IYRgGwFMUQAVQhGEAXyA'
);

cy.title().should('eq', 'Test | Playroom');
});
});
});
144 changes: 60 additions & 84 deletions src/Playroom/FramesPanel/FramesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@ import { ToolbarPanel } from '../ToolbarPanel/ToolbarPanel';
import { StoreContext } from '../../StoreContext/StoreContext';
import { Stack } from '../Stack/Stack';
import { Text } from '../Text/Text';
import { Helmet } from 'react-helmet';
import { Inline } from '../Inline/Inline';
import { Box } from '../Box/Box';

import * as styles from './FramesPanel.css';

const getTitle = (title: string | undefined) => {
if (title) {
return `${title} | Playroom`;
}

const configTitle = window?.__playroomConfig__.title;

if (configTitle) {
return `${configTitle} | Playroom`;
}

return 'Playroom';
};

interface FramesPanelProps {
availableWidths: number[];
availableThemes: string[];
Expand Down Expand Up @@ -104,93 +89,84 @@ export default ({ availableWidths, availableThemes }: FramesPanelProps) => {
const hasFilteredThemes =
visibleThemes.length > 0 && visibleThemes.length <= availableThemes.length;

const displayedTitle = getTitle(title);

return (
<>
{title === undefined ? null : (
<Helmet>
<title>{displayedTitle}</title>
</Helmet>
)}
<ToolbarPanel>
<Stack space="xxxlarge">
<label>
<Stack space="small">
<Heading level="3">Title</Heading>
<input
type="text"
id="playroomTitleField"
placeholder="Enter a title for this Playroom..."
className={styles.textField}
value={title}
onChange={(e) =>
<ToolbarPanel>
<Stack space="xxxlarge">
<label>
<Stack space="small">
<Heading level="3">Title</Heading>
<input
type="text"
id="playroomTitleField"
placeholder="Enter a title for this Playroom..."
className={styles.textField}
value={title}
onChange={(e) =>
dispatch({
type: 'updateTitle',
payload: { title: e.target.value },
})
}
/>
</Stack>
</label>

<Stack space="xlarge">
<FrameHeading
showReset={hasFilteredWidths}
onReset={() => dispatch({ type: 'resetVisibleWidths' })}
>
Widths
</FrameHeading>
{availableWidths.map((option) => (
<FrameOption
key={option}
option={option}
selected={hasFilteredWidths && visibleWidths.includes(option)}
visible={visibleWidths}
onChange={(newWidths) => {
if (newWidths) {
dispatch({
type: 'updateTitle',
payload: { title: e.target.value },
})
type: 'updateVisibleWidths',
payload: { widths: newWidths },
});
} else {
dispatch({ type: 'resetVisibleWidths' });
}
/>
</Stack>
</label>
}}
/>
))}
</Stack>

{hasThemes ? (
<Stack space="xlarge">
<FrameHeading
showReset={hasFilteredWidths}
onReset={() => dispatch({ type: 'resetVisibleWidths' })}
showReset={hasFilteredThemes}
onReset={() => dispatch({ type: 'resetVisibleThemes' })}
>
Widths
Themes
</FrameHeading>
{availableWidths.map((option) => (
{availableThemes.map((option) => (
<FrameOption
key={option}
option={option}
selected={hasFilteredWidths && visibleWidths.includes(option)}
visible={visibleWidths}
onChange={(newWidths) => {
if (newWidths) {
selected={hasFilteredThemes && visibleThemes.includes(option)}
visible={visibleThemes}
onChange={(newThemes) => {
if (newThemes) {
dispatch({
type: 'updateVisibleWidths',
payload: { widths: newWidths },
type: 'updateVisibleThemes',
payload: { themes: newThemes },
});
} else {
dispatch({ type: 'resetVisibleWidths' });
dispatch({ type: 'resetVisibleThemes' });
}
}}
/>
))}
</Stack>

{hasThemes ? (
<Stack space="xlarge">
<FrameHeading
showReset={hasFilteredThemes}
onReset={() => dispatch({ type: 'resetVisibleThemes' })}
>
Themes
</FrameHeading>
{availableThemes.map((option) => (
<FrameOption
key={option}
option={option}
selected={hasFilteredThemes && visibleThemes.includes(option)}
visible={visibleThemes}
onChange={(newThemes) => {
if (newThemes) {
dispatch({
type: 'updateVisibleThemes',
payload: { themes: newThemes },
});
} else {
dispatch({ type: 'resetVisibleThemes' });
}
}}
/>
))}
</Stack>
) : null}
</Stack>
</ToolbarPanel>
</>
) : null}
</Stack>
</ToolbarPanel>
);
};
22 changes: 22 additions & 0 deletions src/Playroom/Playroom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useContext, type ComponentType, Fragment } from 'react';
import { Helmet } from 'react-helmet';
import classnames from 'classnames';
import { useDebouncedCallback } from 'use-debounce';
import { Resizable } from 're-resizable';
Expand Down Expand Up @@ -45,6 +46,20 @@ const resolveDirection = (
return editorHidden ? 'up' : 'down';
};

const getTitle = (title: string | undefined) => {
if (title) {
return `${title} | Playroom`;
}

const configTitle = window?.__playroomConfig__.title;

if (configTitle) {
return `${configTitle} | Playroom`;
}

return 'Playroom';
};

export interface PlayroomProps {
components: Record<string, ComponentType>;
themes: string[];
Expand All @@ -65,9 +80,11 @@ export default ({ components, themes, widths, snippets }: PlayroomProps) => {
previewRenderCode,
previewEditorCode,
ready,
title,
},
dispatch,
] = useContext(StoreContext);
const displayedTitle = getTitle(title);

const updateEditorSize = useDebouncedCallback(
({
Expand Down Expand Up @@ -154,6 +171,11 @@ export default ({ components, themes, widths, snippets }: PlayroomProps) => {

return (
<div className={styles.root}>
{title === undefined ? null : (
<Helmet>
<title>{displayedTitle}</title>
</Helmet>
)}
<div
className={styles.previewContainer}
style={
Expand Down