Skip to content

Commit

Permalink
feat(docs): open in stackblitz (#1593)
Browse files Browse the repository at this point in the history
* chore(docs): open in stackblitz

* chore: update code

* chore: update code

* refactor: logic and expose demo api

* docs: fix

* fix: docs

---------

Co-authored-by: Segun Adebayo <[email protected]>
  • Loading branch information
anubra266 and segunadebayo authored Sep 10, 2024
1 parent 4465743 commit 203f21c
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/next-ts/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website/components/machines/progress-circular.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as progress from "@zag-js/progress"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"

export function CircularProgress(props: any) {
export function ProgressCircular(props: any) {
const [state, send] = useMachine(progress.machine({ id: useId() }), {
context: props.controls,
})
Expand Down
2 changes: 1 addition & 1 deletion website/components/machines/progress-linear.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as progress from "@zag-js/progress"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"

export function LinearProgress(props: any) {
export function ProgressLinear(props: any) {
const [state, send] = useMachine(progress.machine({ id: useId() }), {
context: props.controls,
})
Expand Down
2 changes: 1 addition & 1 deletion website/components/machines/qr-code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as qrCode from "@zag-js/qr-code"
import { normalizeProps, useMachine } from "@zag-js/react"
import { useId } from "react"

export function QRCode(props: any) {
export function QrCode(props: any) {
const [state, send] = useMachine(
qrCode.machine({
id: useId(),
Expand Down
15 changes: 11 additions & 4 deletions website/components/mdx-components.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { allComponents, allSnippets } from "@/contentlayer"
import { Icon } from "@chakra-ui/icon"
import { Box, HStack, Wrap } from "@chakra-ui/layout"
import { Box, HStack, StackProps, Wrap } from "@chakra-ui/layout"
import { chakra } from "@chakra-ui/system"
import { allComponents as Anatomies } from "@zag-js/anatomy-icons"
import { normalizeProps, useMachine } from "@zag-js/react"
Expand Down Expand Up @@ -34,12 +33,17 @@ function SnippetItem({ body, id }: { body: MDX; id: string }) {
}

type ResourceLinkProps = {
href: string | undefined
href?: string | undefined
icon: FC
children: any
}

export function ResourceLink({ href, icon, children }: ResourceLinkProps) {
export function ResourceLink({
href,
icon,
children,
...rest
}: ResourceLinkProps & StackProps) {
return (
<HStack
as="a"
Expand All @@ -50,6 +54,8 @@ export function ResourceLink({ href, icon, children }: ResourceLinkProps) {
py="1"
fontSize="sm"
spacing="1"
cursor="pointer"
{...rest}
>
<Icon as={icon} color="green.500" fontSize="lg" />
<span>{children}</span>
Expand All @@ -71,6 +77,7 @@ const components: Record<string, FC<any>> = {
},
Resources(props) {
const comp = allComponents.find((c) => c.package === props.pkg)

if (!comp) return null
return (
<Wrap mt="6" spacingX="4">
Expand Down
1 change: 1 addition & 0 deletions website/components/mutli-framework.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function MultiframeworkTabs() {
}}
>
<Playground
name="number-input"
component={NumberInput}
hideControls
defaultContext={{ value: 0 }}
Expand Down
45 changes: 43 additions & 2 deletions website/components/playground.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Box, Flex, Stack } from "@chakra-ui/layout"
import { Box, Flex, HStack, Stack } from "@chakra-ui/layout"
import { chakra } from "@chakra-ui/system"
import { openInStackblitz } from "lib/open-in-stackblitz"
import { useState } from "react"
import { SiStackblitz } from "react-icons/si"

const Header = (props: any) => (
<Flex
Expand All @@ -16,6 +18,7 @@ const Header = (props: any) => (
)

type PlaygroundProps = {
name: string
component: (props: any) => JSX.Element
defaultContext?: Record<string, any>
defaultProps?: Record<
Expand All @@ -29,8 +32,41 @@ type PlaygroundProps = {
debug?: boolean
}

const isObject = (value: any): value is Record<string, any> =>
typeof value === "object" && value !== null && !Array.isArray(value)

const OpenInStackblitz = (props: { name: string; defaultProps: any }) => {
const { name } = props
const defaultProps = Object.fromEntries(
Object.entries(props.defaultProps).map(([key, value]) => [
key,
isObject(value) ? value.default : value,
]),
)

return (
<HStack
spacing="1"
as="button"
bg="#1574ef"
color="white"
fontSize="sm"
px="2"
py="1"
shadow="rgba(255, 255, 255, 0.14) 0px 0px 0px 1px inset"
onClick={() => {
openInStackblitz(name, defaultProps)
}}
>
<SiStackblitz />
<p>Stackblitz</p>
</HStack>
)
}

export function Playground(props: PlaygroundProps) {
const {
name: componentName,
component: Comp,
defaultProps = {},
debug,
Expand All @@ -54,11 +90,16 @@ export function Playground(props: PlaygroundProps) {
id="playground"
direction={{ base: "column", md: "row" }}
borderWidth="1px"
pos="relative"
minHeight="24em"
my="16"
bg="bg-code-block"
borderColor="border-subtle"
>
<Box pos="absolute" bottom="2" right="2">
<OpenInStackblitz name={componentName} defaultProps={defaultProps} />
</Box>

<Flex
align="flex-start"
justify="center"
Expand All @@ -83,7 +124,7 @@ export function Playground(props: PlaygroundProps) {
hidden={isEmpty}
>
<Header>Properties</Header>
<Stack direction="column" spacing="4" px="5" py="4">
<Stack pos="relative" direction="column" spacing="4" px="5" py="4">
{Object.keys(state).map((key) => {
const value = state[key]
const type = defaultProps[key]
Expand Down
Loading

0 comments on commit 203f21c

Please sign in to comment.