-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[polaris.shopify.com] Include HTML source for examples #6688
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
Changes from 22 commits
98b6bbc
6196272
675e3ca
a6b3aae
510fd2e
0b3bd78
42fa893
a99a605
72109a1
bce992c
b10dac4
467e6a6
41397ed
cba2ffb
29953db
a252b7a
5b8edf2
929da6b
9fcdf0a
68d2694
ea746ca
14d50e8
d14db48
27fcb14
4b68771
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| @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 { | ||
| 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; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| 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 { | ||
| tabs: { | ||
| title: string; | ||
| code: string; | ||
| }[]; | ||
| } | ||
|
|
||
| function Code({ tabs }: Props) { | ||
|
||
| const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
|
||
| return ( | ||
| <div className={styles.Code}> | ||
| {tabs.length === 1 ? ( | ||
| <> | ||
| <div className={styles.TopBar}> | ||
| <div className={styles.Tabs}> | ||
| <div className={styles.Tab}>{tabs[0].title}</div> | ||
| </div> | ||
| <CopyButton code={tabs[0].code} /> | ||
| </div> | ||
| <HighlightedCode code={tabs[0].code} /> | ||
| </> | ||
| ) : ( | ||
| <Tab.Group selectedIndex={selectedIndex} onChange={setSelectedIndex}> | ||
| <div className={styles.TopBar}> | ||
| <Tab.List className={styles.Tabs}> | ||
| {tabs.map(({ title }) => ( | ||
| <Tab key={title} className={styles.Tab}> | ||
| {title} | ||
| </Tab> | ||
| ))} | ||
| </Tab.List> | ||
| {tabs[selectedIndex] && ( | ||
| <CopyButton code={tabs[selectedIndex].code} /> | ||
| )} | ||
| </div> | ||
|
|
||
| <Tab.Panels> | ||
| {tabs.map(({ title, code }) => ( | ||
| <Tab.Panel key={title}> | ||
| <HighlightedCode code={code} /> | ||
| </Tab.Panel> | ||
| ))} | ||
| </Tab.Panels> | ||
| </Tab.Group> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function HighlightedCode({ code }: { code: string }) { | ||
| return ( | ||
| <div | ||
| className={styles.ActualCode} | ||
| dangerouslySetInnerHTML={{ | ||
| __html: Prism.highlight(code, Prism.languages.javascript, "javasript"), | ||
| }} | ||
| ></div> | ||
|
||
| ); | ||
| } | ||
|
|
||
| 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; | ||
| 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.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.