-
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
Merged
Merged
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 6196272
Design tweaks
martenbjork 675e3ca
Remove unused import
martenbjork a6b3aae
Make button text strong
martenbjork 510fd2e
Show HTML source next to React source
martenbjork 0b3bd78
Merge branch 'main' into show-html-source
martenbjork 42fa893
Use the same Code component to render any code block on the site
martenbjork a99a605
Add changeset
martenbjork 72109a1
Fix border color
martenbjork bce992c
Fix link color
martenbjork b10dac4
Add missing alt attribute
martenbjork 467e6a6
Remove code formatting from longform component
martenbjork 41397ed
Remove console log
martenbjork cba2ffb
Use implicit returns
martenbjork 29953db
Remove wrapper
martenbjork a252b7a
Rename Examples component
martenbjork 5b8edf2
Implement copy code button
martenbjork 929da6b
Remove import
martenbjork 9fcdf0a
Fix typing
martenbjork 68d2694
Update .changeset/nine-goats-leave.md
martenbjork ea746ca
Update polaris.shopify.com/src/components/ComponentExamples/Component…
martenbjork 14d50e8
Merge branch 'main' into show-html-source
martenbjork d14db48
Refactor code component
martenbjork 27fcb14
Use more semantic tags for code
martenbjork 4b68771
Merge branch 'main' into show-html-source
martenbjork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
103
polaris.shopify.com/src/components/Code/Code.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ), | ||
| }} | ||
| ></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; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import Code from "./Code"; | ||
|
|
||
| export default Code; |
88 changes: 0 additions & 88 deletions
88
polaris.shopify.com/src/components/CodeExample/CodeExample.module.scss
This file was deleted.
Oops, something went wrong.
73 changes: 0 additions & 73 deletions
73
polaris.shopify.com/src/components/CodeExample/CodeExample.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 :)
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.
Fixed the typo here b3e14b7
Are you able to write up an issue around what
Prism.manual = truewould change or enhance with the code highlighting? I am taking a look and am not too sure I understand if it's needed.