Skip to content

Commit

Permalink
improve flex grid preview
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Oct 31, 2023
1 parent 82438b0 commit 6936111
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
6 changes: 6 additions & 0 deletions components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useContext } from "preact/hooks";
import { createContext } from "preact";

export const LayoutContext = createContext({ isPreview: false });

export const useLayoutContext = () => useContext(LayoutContext);
13 changes: 11 additions & 2 deletions sections/Gallery.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { Section } from "deco/blocks/section.ts";
import { LayoutContext } from "$store/components/Layout.tsx";

interface Props {
children: Section;
}

function Gallery({ children: { Component, props } }: Props) {
function Section({ children: { Component, props } }: Props) {
return (
<>
<Component {...props} />
</>
);
}

export default Gallery;
export function Preview(props: Props) {
return (
<LayoutContext.Provider value={{ isPreview: true }}>
<Section {...props} />
</LayoutContext.Provider>
);
}

export default Section;
47 changes: 27 additions & 20 deletions sections/Layout/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { Section } from "deco/blocks/section.ts";
import { LayoutContext, useLayoutContext } from "$store/components/Layout.tsx";

interface Props {
children?: Section;
}

function Container({ children }: Props) {
if (!children) return null;
function Placeholder() {
return (
<div class="rounded h-48 grid place-content-center w-full bg-base-100 text-base-300 text-sm">
Content
</div>
);
}

function Section({ children }: Props) {
const { isPreview } = useLayoutContext();

if (isPreview && typeof children?.Component !== "function") {
return (
<div class="bg-primary bg-opacity-5 p-4">
<Section children={{ Component: Placeholder, props: {} }} />
</div>
);
}

if (!children) {
return null;
}

return (
<div class="container">
Expand All @@ -15,25 +36,11 @@ function Container({ children }: Props) {
}

export function Preview(props: Props) {
if (props.children) {
return <Container {...props} />;
}

return (
<div class="bg-primary bg-opacity-5 p-4">
<Container
{...props}
children={{
Component: () => (
<div class="rounded h-48 grid place-content-center w-full bg-base-100 text-base-300 text-sm">
Content
</div>
),
props: {},
}}
/>
</div>
<LayoutContext.Provider value={{ isPreview: true }}>
<Section {...props} />
</LayoutContext.Provider>
);
}

export default Container;
export default Section;
18 changes: 15 additions & 3 deletions sections/Layout/Flex.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LayoutContext, useLayoutContext } from "$store/components/Layout.tsx";
import { clx } from "$store/sdk/clx.ts";
import { Section } from "deco/blocks/section.ts";
import { flex, VNode } from "../../constants.tsx";
Expand Down Expand Up @@ -33,6 +34,11 @@ interface Props {
}

function Section({ layout, children }: Props) {
const { isPreview } = useLayoutContext();
const items = isPreview && !children?.length
? new Array(4).fill(0).map(() => <ItemPreview />)
: children;

return (
<div
class={clx(
Expand All @@ -47,13 +53,19 @@ function Section({ layout, children }: Props) {
flex.wrap.desktop[layout?.wrap?.desktop ?? "wrap"],
)}
>
{children?.length
? children
: new Array(4).fill(0).map(() => <ItemPreview />)}
{items}
</div>
);
}

export function Preview(props: Props) {
return (
<LayoutContext.Provider value={{ isPreview: true }}>
<Section {...props} />
</LayoutContext.Provider>
);
}

const ItemPreview = () => (
<div class="card w-48 h-48 bg-base-100 shadow">
<div class="card-body items-center justify-center text-base-300 text-sm">
Expand Down
18 changes: 15 additions & 3 deletions sections/Layout/Grid.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Section } from "deco/blocks/section.ts";
import { grid, VNode } from "../../constants.tsx";
import { clx } from "../../sdk/clx.ts";
import { LayoutContext, useLayoutContext } from "$store/components/Layout.tsx";

interface Props {
children?: VNode[] | null;
Expand Down Expand Up @@ -65,6 +66,11 @@ interface Props {
}

function Section({ layout, children }: Props) {
const { isPreview } = useLayoutContext();
const items = isPreview && !children?.length
? new Array(12).fill(0).map(() => <ItemPreview />)
: children;

return (
<div
class={clx(
Expand All @@ -81,13 +87,19 @@ function Section({ layout, children }: Props) {
grid.placeItems.desktop[layout?.placeItems?.desktop ?? "center"],
)}
>
{children?.length
? children
: new Array(12).fill(0).map(() => <ItemPreview />)}
{items}
</div>
);
}

export function Preview(props: Props) {
return (
<LayoutContext.Provider value={{ isPreview: true }}>
<Section {...props} />
</LayoutContext.Provider>
);
}

const ItemPreview = () => (
<div class="card w-48 h-48 bg-base-100 shadow">
<div class="card-body items-center justify-center text-base-300 text-sm">
Expand Down

2 comments on commit 6936111

@deno-deploy
Copy link
Contributor

@deno-deploy deno-deploy bot commented on 6936111 Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An internal server error occurred.

@deno-deploy
Copy link
Contributor

@deno-deploy deno-deploy bot commented on 6936111 Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An internal server error occurred.

Please sign in to comment.