Skip to content

Commit

Permalink
fix: expandable code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
josefinebrorson committed Jul 4, 2023
1 parent 7a3827f commit 9dd706d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/webapp/layout/story/Story.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from "@emotion/styled";
import { Center, Title } from "@mantine/core";
import { Center, Title, Button, Collapse } from "@mantine/core";
import { observer } from "mobx-react-lite";
import { Story } from "../../../types";
import { Markdown } from "./atom/Markdown";
Expand All @@ -10,6 +10,8 @@ import { useControl } from "./control/useControl";
import { useIsDarkMode } from "./control/useIsDarkMode";
import { DefaultComponentWrapper } from "./DefaultComponentWrapper";
import { useActionOutput } from "./useActionOutput";
import { useState } from "react";
import { ArrowDownIcon } from "../../icons/ArrowDownIcon";

export const StoryComponent = observer(({ story }: { story: Story }) => {
const controls = useControl();
Expand All @@ -30,6 +32,16 @@ export const StoryComponent = observer(({ story }: { story: Story }) => {

const sectionId = encodeURIComponent(story?.name ?? "");

const [opened, setOpen] = useState(false);

const toggleCodeSnippet = () => {
setOpen((prevOpened) => !prevOpened);
};

const handleClick = () => {
toggleCodeSnippet();
};

return (
<Wrapper>
<SectionAnchor id={sectionId}>
Expand Down Expand Up @@ -62,10 +74,35 @@ export const StoryComponent = observer(({ story }: { story: Story }) => {
</ControlsWrapper>
</Configurator>
{story.codeTemplate && (
<CodeTemplate
codeTemplate={story.codeTemplate}
controls={controls}
/>
<>
<Button
leftIcon={<ArrowDownIcon />}
variant="default"
onClick={handleClick}
styles={() => ({
root: {
color: "gray",
margin: "20px 0",
},
leftIcon: {
transform: opened ? "rotate(180deg)" : "rotate(0deg)",
transition: "transform 0.3s ease-in-out",
},
})}
>
{opened ? "Hide code example" : "Show code example"}
</Button>
<Collapse
in={opened}
transitionDuration={300}
transitionTimingFunction="linear"
>
<CodeTemplate
codeTemplate={story.codeTemplate}
controls={controls}
/>
</Collapse>
</>
)}
<ActionOutput outputs={outputs} />
</>
Expand Down
33 changes: 33 additions & 0 deletions src/webapp/layout/story/story-button/StoryButton.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const story: Story = {
{
name: "Kivra Design System",
center: true,

render() {
return (
<StoryHeaderButton
Expand All @@ -33,5 +34,37 @@ export const story: Story = {
);
},
},
{
name: "Story Button",
center: true,
information:
"This is just an example how to use `<StoryHeaderButton />`s props. <br/>In this example also implemented show/hide code snippet that is possible to use in stories with `codeTemplate` function.",
codeTemplate: (props) => `
<StoryHeaderButton${props}></StoryHeaderButton>
`,
render({ segment, text }) {
const type = segment({
name: "type",
value: "designsystem",
options: [
["designsystem", "designsystem"],
["github", "github"],
["figma", "figmaa"],
],
showAs: "select",
});
return (
<StoryHeaderButton
type={type}
url={text({
name: "url",
value: "https://",
defaultValue: "https://design.kivra.com/",
description: "Always start with https://",
})}
/>
);
},
},
],
};

0 comments on commit 9dd706d

Please sign in to comment.