-
Notifications
You must be signed in to change notification settings - Fork 324
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
docs: add edit mode for stories in docs page #2043
Merged
YossiSaadi
merged 16 commits into
master
from
docs/yossi/Add-edit-mode-for-Storybooks-Docs-page-Stories-5898424178
Apr 7, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f0fbf6a
docs: add CanvasWrapper component to render a code editor inside it
YossiSaadi d303e16
docs: make CanvasWrapper the default Canvas unless imported in an mdx
YossiSaadi 0fa24e5
docs: add LiveEditor component for editing stories
YossiSaadi 0e4a32e
docs: add withLiveEdit decorator to wrap all stories with a decorator…
YossiSaadi 01dd01d
docs: encapsulate MultipleStoryElementsWrapper into a global style de…
YossiSaadi 479c48d
docs: show default padded layout for story view and iframe view
YossiSaadi 9da8856
docs: allow disabling entire live edit experience for specific story
YossiSaadi b3c9b1f
docs: format code on initial load of editor
YossiSaadi 612ad57
docs: make the indicator have no pointer events
YossiSaadi 125815d
docs(Button): add edit experience for button stories
YossiSaadi ad9e946
docs(Button): disable live edit for overview story, add Calendar icon…
YossiSaadi 1777869
docs(withLiveEdit): do not fail when prettier fails to format
YossiSaadi 51a1730
refactor: rename to isEnabled
YossiSaadi db9360b
refactor(withLiveEdit): only render portal of id div exists for story
YossiSaadi 7d86de2
docs(SplitButton): enable LiveEdit
YossiSaadi e40d7de
docs(withLiveEdit): better coding - do not use effect - set on initia…
YossiSaadi 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 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
packages/core/src/components/SplitButton/__stories__/splitButton.mdx
This file contains 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
This file contains 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
28 changes: 28 additions & 0 deletions
28
packages/core/src/storybook/components/canvas-wrapper/CanvasWrapper.module.scss
This file contains 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,28 @@ | ||
.editorContainer { | ||
position: relative; | ||
top: -40px; | ||
width: 100%; | ||
background: transparent; | ||
max-height: 0; | ||
opacity: 0; | ||
transition: max-height 0.1s ease-out, opacity 0.1s ease-out; | ||
overflow: auto; | ||
border-radius: 0 0 4px 4px; | ||
box-shadow: rgba(0, 0, 0, 0.1) 0 1px 3px 0; | ||
border: 1px solid hsla(203, 50%, 30%, 0.15); | ||
border-top: none; | ||
|
||
&[data-editor-open="true"] { | ||
max-height: 500px; | ||
opacity: 1; | ||
} | ||
|
||
& :global(.cm-editor) { | ||
padding: var(--sb-spacing-medium); | ||
} | ||
} | ||
|
||
.canvas:has(+ [data-editor-open="true"]) { | ||
border-bottom-left-radius: 0; | ||
border-bottom-right-radius: 0; | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/core/src/storybook/components/canvas-wrapper/CanvasWrapper.tsx
This file contains 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,36 @@ | ||
import { Canvas, useOf } from "@storybook/blocks"; | ||
import { ComponentProps, FC, useMemo, useState } from "react"; | ||
import styles from "./CanvasWrapper.module.scss"; | ||
|
||
type CanvasWrapper = ComponentProps<typeof Canvas>; | ||
|
||
const CanvasWrapper: FC<CanvasWrapper> = ({ of }) => { | ||
const [open, setOpen] = useState<boolean>(false); | ||
|
||
// resolve Storybook story module to get the story id and parameters of current rendered story | ||
const { story } = useOf(of, ["story"]); | ||
|
||
const toggleCodeAction = useMemo( | ||
() => ({ | ||
title: "Toggle code editor", | ||
onClick: () => setOpen(prev => !prev) | ||
}), | ||
[] | ||
); | ||
|
||
const liveEditEnabledForStory = story.parameters.docs?.liveEdit?.isEnabled; | ||
|
||
return ( | ||
<> | ||
<Canvas | ||
of={of} | ||
sourceState={liveEditEnabledForStory ? "none" : "hidden"} | ||
additionalActions={liveEditEnabledForStory ? [toggleCodeAction] : []} | ||
className={styles.canvas} | ||
/> | ||
{liveEditEnabledForStory && <div id={story.id} className={styles.editorContainer} data-editor-open={open} />} | ||
</> | ||
); | ||
}; | ||
|
||
export default CanvasWrapper; |
34 changes: 34 additions & 0 deletions
34
packages/core/src/storybook/components/live-editor/LiveEditor.tsx
This file contains 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,34 @@ | ||
import React, { forwardRef } from "react"; | ||
import CodeMirror, { Extension, ReactCodeMirrorRef, BasicSetupOptions } from "@uiw/react-codemirror"; | ||
|
||
interface EditorProps { | ||
code: string; | ||
onChange: (newVal: string) => void; | ||
placeholder?: string; | ||
loading?: boolean; | ||
theme?: "light" | "dark" | Extension; | ||
style?: React.CSSProperties; | ||
extensions?: Extension[]; | ||
setup?: BasicSetupOptions; | ||
} | ||
|
||
type EditorComponent = React.ForwardRefExoticComponent<EditorProps & React.RefAttributes<ReactCodeMirrorRef>>; | ||
|
||
const LiveEditor: EditorComponent = forwardRef( | ||
({ code, onChange, placeholder, theme = "light", style, extensions, setup }, ref) => { | ||
return ( | ||
<CodeMirror | ||
style={style} | ||
ref={ref} | ||
theme={theme} | ||
value={code} | ||
extensions={extensions} | ||
onChange={onChange} | ||
placeholder={placeholder} | ||
basicSetup={setup} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
export default LiveEditor; |
16 changes: 16 additions & 0 deletions
16
packages/core/src/storybook/components/live-preview/LivePreview.tsx
This file contains 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,16 @@ | ||
import React from "react"; | ||
import { withLive } from "react-live"; | ||
|
||
interface LivePreview { | ||
live?: { | ||
// live injects more, currently unneeded, props | ||
error?: string; | ||
element?: React.ComponentType; | ||
}; | ||
} | ||
|
||
const LivePreview: React.FC<LivePreview> = ({ live = {} }) => { | ||
const { error, element: Element } = live; | ||
return <>{error ?? (Element && <Element />)}</>; | ||
}; | ||
export default withLive(LivePreview); |
3 changes: 3 additions & 0 deletions
3
packages/core/src/storybook/decorators/withGlobalStyle/withGlobalStyle.module.scss
This file contains 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 @@ | ||
.storyWrapper { | ||
padding: var(--spacing-xl) var(--spacing-large); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/core/src/storybook/decorators/withGlobalStyle/withGlobalStyle.tsx
This file contains 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,14 @@ | ||
import { MultipleStoryElementsWrapper } from "vibe-storybook-components"; | ||
import cx from "classnames"; | ||
import styles from "./withGlobalStyle.module.scss"; | ||
import { Decorator } from "@storybook/react"; | ||
|
||
const WithGlobalStyle: Decorator = (Story, { className, viewMode }) => { | ||
return ( | ||
<MultipleStoryElementsWrapper className={cx({ [styles.storyWrapper]: viewMode === "docs" }, className)}> | ||
<Story /> | ||
</MultipleStoryElementsWrapper> | ||
); | ||
}; | ||
|
||
export default WithGlobalStyle; |
17 changes: 17 additions & 0 deletions
17
packages/core/src/storybook/decorators/withLiveEdit/prettier-utils.ts
This file contains 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,17 @@ | ||
import { format, Options } from "prettier"; | ||
import parserHtml from "prettier/parser-html"; | ||
import parserTypeScript from "prettier/parser-typescript"; | ||
|
||
export function formatCode(code: string): string { | ||
const options: Options = { | ||
parser: "typescript", | ||
arrowParens: "avoid", | ||
trailingComma: "es5", | ||
plugins: [parserHtml, parserTypeScript] | ||
}; | ||
try { | ||
return format(code, options).replace(/;\s*$/, ""); | ||
} catch (e) { | ||
throw new Error(`[LiveEdit Error]: Error formatting code: ${e}`); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/core/src/storybook/decorators/withLiveEdit/withLiveEdit.module.scss
This file contains 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,13 @@ | ||
.modifiedVersionIndicator { | ||
position: absolute; | ||
top: 0; | ||
left: calc(50% - var(--sb-spacing-large)); | ||
margin: 0; | ||
border-radius: 0 0 4px 4px; | ||
background: var(--sb-primary-color); | ||
color: var(--sb-text-color-on-primary); | ||
padding: var(--sb-spacing-xs); | ||
font-size: 10px; | ||
pointer-events: none; | ||
user-select: none; | ||
} |
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.
Suggestion: call it "Play"
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.
Just like that? Play?
It opens and shut the live editor, not sure about Play