Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
98b6bbc
Improve examples browsing experience
martenbjork Jul 19, 2022
6196272
Design tweaks
martenbjork Jul 19, 2022
675e3ca
Remove unused import
martenbjork Jul 19, 2022
a6b3aae
Make button text strong
martenbjork Jul 20, 2022
510fd2e
Show HTML source next to React source
martenbjork Jul 20, 2022
0b3bd78
Merge branch 'main' into show-html-source
martenbjork Jul 21, 2022
42fa893
Use the same Code component to render any code block on the site
martenbjork Jul 21, 2022
a99a605
Add changeset
martenbjork Jul 21, 2022
72109a1
Fix border color
martenbjork Jul 21, 2022
bce992c
Fix link color
martenbjork Jul 21, 2022
b10dac4
Add missing alt attribute
martenbjork Jul 21, 2022
467e6a6
Remove code formatting from longform component
martenbjork Jul 21, 2022
41397ed
Remove console log
martenbjork Jul 21, 2022
cba2ffb
Use implicit returns
martenbjork Jul 21, 2022
29953db
Remove wrapper
martenbjork Jul 21, 2022
a252b7a
Rename Examples component
martenbjork Jul 21, 2022
5b8edf2
Implement copy code button
martenbjork Jul 21, 2022
929da6b
Remove import
martenbjork Jul 21, 2022
9fcdf0a
Fix typing
martenbjork Jul 21, 2022
68d2694
Update .changeset/nine-goats-leave.md
martenbjork Jul 21, 2022
ea746ca
Update polaris.shopify.com/src/components/ComponentExamples/Component…
martenbjork Jul 21, 2022
14d50e8
Merge branch 'main' into show-html-source
martenbjork Jul 21, 2022
d14db48
Refactor code component
martenbjork Jul 22, 2022
27fcb14
Use more semantic tags for code
martenbjork Jul 22, 2022
4b68771
Merge branch 'main' into show-html-source
martenbjork Jul 22, 2022
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/nine-goats-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'polaris.shopify.com': minor
---

Simplified rendering of code blocks on site by using the same component for every code example. This makes the experience more consistent and ensures that every code block has features like copy/paste.
103 changes: 103 additions & 0 deletions polaris.shopify.com/src/components/Code/Code.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
@import "../../styles/mixins.scss";

.Code {
border-radius: var(--border-radius-400);
max-width: calc(100vw - 1.5rem);
cursor: text;
background: var(--surface);
color: var(--text-strong);
box-shadow: var(--card-shadow);
}

.TopBar {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid var(--border-color-light);
}

.Tabs {
padding-left: 1rem;
display: flex;

.Tab {
padding-top: 0.66rem;
padding-bottom: 0.66rem;
border-radius: var(--border-radius-300);
background: transparent;
font-size: var(--font-size-100);
color: var(--text-subdued);
}

button.Tab {
padding-left: 0.5rem;
padding-right: 0.5rem;
color: var(--text-strong);

&[aria-selected="true"] {
position: relative;

&:not(:focus-visible) {
&:after {
content: "";
display: block;
position: absolute;
right: 0;
bottom: 0;
left: 0;
background: var(--text-strong);
height: 3px;
border-radius: var(--border-radius-200) var(--border-radius-200) 0 0;
}
}
}
}

&:focus-visible {
box-shadow: var(--focus-outline);
}
}

.CopyButton {
padding: 0.5rem 1rem;
background: transparent;
border-radius: var(--border-radius-200);
opacity: 0.5;
display: flex;
align-items: center;

&:hover,
&:focus {
opacity: 1;
}

svg {
width: 16px;
height: 16px;
}

&:focus-visible {
box-shadow: none;
opacity: 1;
}
}

.ActualCode {
display: block;
font-family: var(--font-family-mono);
white-space: pre-wrap;
padding: 0.85rem 1rem;
font-size: 14px;
line-height: 1.6;
overflow: auto;
max-height: 50vh;

@include custom-scrollbars;
}

.LineNumber {
display: none;
opacity: 0.5;
margin-right: 0.5rem;
user-select: none;
}
109 changes: 109 additions & 0 deletions polaris.shopify.com/src/components/Code/Code.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import Tooltip from "../Tooltip";
import Prism from "prismjs";
import { useCopyToClipboard } from "../../utils/hooks";
import styles from "./Code.module.scss";
import { Tab } from "@headlessui/react";
import Image from "../Image";
import { useState } from "react";

interface Props {
code:
| {
title: string;
code: string;
}
| {
title: string;
code: string;
}[];
}

function Code({ code }: Props) {
const [selectedIndex, setSelectedIndex] = useState(0);

return (
<div className={styles.Code}>
{Array.isArray(code) ? (
<Tab.Group selectedIndex={selectedIndex} onChange={setSelectedIndex}>
<div className={styles.TopBar}>
<Tab.List className={styles.Tabs}>
{code.map(({ title }) => (
<Tab key={title} className={styles.Tab}>
{title}
</Tab>
))}
</Tab.List>
{code[selectedIndex] && (
<CopyButton code={code[selectedIndex].code} />
)}
</div>

<Tab.Panels>
{code.map(({ title, code }) => (
<Tab.Panel key={title}>
<HighlightedCode code={code} />
</Tab.Panel>
))}
</Tab.Panels>
</Tab.Group>
) : (
<>
<div className={styles.TopBar}>
<div className={styles.Tabs}>
<div className={styles.Tab}>{code.title}</div>
</div>
<CopyButton code={code.code} />
</div>
<HighlightedCode code={code.code} />
</>
)}
</div>
);
}

function HighlightedCode({ code }: { code: string }) {
return (
<pre>
<code
className={styles.ActualCode}
dangerouslySetInnerHTML={{
__html: Prism.highlight(
code,
Prism.languages.javascript,
"javasript"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a typo!

also with Prism, it’s best practice to set Prism.manual = true somewhere at the top of this file. It shouldn’t make a difference here because you haven’t used the classes on the containing element that prism searches for to replace the content, but, that can get funky. Prism.manual is also just a flag for the internals of prism not to run unless called so it’s a minor optimization but worth it :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the typo here b3e14b7

Are you able to write up an issue around what Prism.manual = true would change or enhance with the code highlighting? I am taking a look and am not too sure I understand if it's needed.

),
}}
></code>
</pre>
);
}

function CopyButton({ code }: { code: string }) {
const [copy, didJustCopy] = useCopyToClipboard(code);

return (
<div className={styles.CopyButtonWrapper}>
<Tooltip
ariaLabel="Copy to clipboard"
renderContent={() => <p>{didJustCopy ? "Copied" : "Copy"}</p>}
>
<button
type="button"
className={styles.CopyButton}
onClick={copy}
aria-label="Copy to clipboard"
>
<Image
src="/icons/ClipboardMinor.svg"
alt="Clipboard icon"
width={16}
height={16}
icon
/>
</button>
</Tooltip>
</div>
);
}

export default Code;
3 changes: 3 additions & 0 deletions polaris.shopify.com/src/components/Code/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Code from "./Code";

export default Code;

This file was deleted.

73 changes: 0 additions & 73 deletions polaris.shopify.com/src/components/CodeExample/CodeExample.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions polaris.shopify.com/src/components/CodeExample/index.ts

This file was deleted.

Loading