Skip to content

Commit

Permalink
chore: updated plugins to use context value instead of children props
Browse files Browse the repository at this point in the history
  • Loading branch information
henrynoowah committed Jan 10, 2025
1 parent 181e299 commit d344caf
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 193 deletions.
9 changes: 6 additions & 3 deletions apps/docs/stories/plugins.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { SettingsButton } from "@noowah/content-builder/editor/plugins/settings"

const meta: Meta<typeof Editor> = {
title: "UI/Plugins",
component: Editor,
component: (props) => (
<Editor {...props}>
<SettingsButton />
</Editor>
),
decorators: [(Story) => <Story />],
tags: ["autodocs"],
parameters: { layout: "full" },
Expand All @@ -15,11 +19,10 @@ export default meta;

type Story = StoryObj<typeof meta>;

export const WithSettings: Story = {
export const Settings: Story = {
args: {
onSubmit: () => {},
onChange: () => {},
children: (props) => <SettingsButton {...props} />,
data,
},
};
48 changes: 0 additions & 48 deletions packages/config-eslint/next.ts

This file was deleted.

198 changes: 107 additions & 91 deletions packages/ui/src/components/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,30 @@ import {
rectSortingStrategy,
SortableContext,
} from "@dnd-kit/sortable";
import { lazy, useRef, useState } from "react";
import { createContext, lazy, useContext, useRef, useState } from "react";
import { FormProvider, useFieldArray, useForm } from "react-hook-form";
import { twMerge } from "tailwind-merge";
import { convertJSONToCSS } from "../../lib";
import type {
Block,
EditorContextProps,
EditorProps,
Page,
Section,
SectionEditor,
} from "../../types";

// Create the EditorContext
const EditorContext = createContext<EditorContextProps | undefined>(undefined);

export const useEditorContext = () => {
const context = useContext(EditorContext);
if (!context) {
throw new Error("useEditorContext must be used within an EditorProvider");
}
return context;
};

const SectionEditor = lazy(() => import("./section-editor"));

const Editor = ({ view = "web", data, onSubmit, children }: EditorProps) => {
Expand Down Expand Up @@ -111,104 +123,108 @@ const Editor = ({ view = "web", data, onSubmit, children }: EditorProps) => {
};

return (
<FormProvider {...methods}>
<form
style={{
...data?.style,
containerName,
maxWidth: view === "web" ? undefined : "768px",
}}
onSubmit={handleSubmit(onSubmitHandler, (err) => console.error(err))}
>
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
<EditorContext.Provider
value={{
data,
selectedSection,
selectedBlock,
updateSelectedSection: updateSectionHandler,
updateSelectedBlock: updateBlockHandler,
addBlock: addBlockHandler,
}}
>
<FormProvider {...methods}>
<form
style={{
...data?.style,
containerName,
maxWidth: view === "web" ? undefined : "768px",
}}
onSubmit={handleSubmit(onSubmitHandler, (err) => console.error(err))}
>
<SortableContext items={sections} strategy={rectSortingStrategy}>
{sections.map((section, i) => {
const css = `
#${section.id} {
${convertJSONToCSS(section.style)}
}
@container ${containerName} (max-width: 768px) {
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
>
<SortableContext items={sections} strategy={rectSortingStrategy}>
{sections.map((section, i) => {
const css = `
#${section.id} {
${convertJSONToCSS(section.style_mobile)}
}
`;
return (
${convertJSONToCSS(section.style)}
}
@container ${containerName} (max-width: 768px) {
#${section.id} {
${convertJSONToCSS(section.style_mobile)}
}
`;
return (
<div
key={`${section.id}`}
onClick={() => setSelectedSection(section)}
style={{
containerType: "inline-size",
containerName: containerName,
}}
// ? Apply container properties
className={twMerge(
"nwcb-w-full nwcb-relative nwcb-ring-2 nwcb-transition nwcb-duration-200 nwcb-ease-in-out",
selectedSection?.id === section.id
? "nwcb-ring-blue-400"
: "nwcb-ring-blue-400/20 hover:nwcb-ring-blue-400"
)}
>
<style>{css}</style>

<SectionEditor
ref={
i ===
sections.findIndex(
(section) => section.id === selectedSection?.id
)
? selectedSectionRef
: undefined
}
key={`${section.id}`}
section={section}
index={i}
onSectionSelect={setSelectedSection}
onSwap={(section) => updateSection(i, section)}
onBlockSelect={setSelectedBlock}
containerName={containerName}
/>
</div>
);
})}
</SortableContext>

<DragOverlay
// modifiers={[snapCenterToCursor]}
// dropAnimation={dropAnimation}
>
{activeId ? (
<div
key={`${section.id}`}
onClick={() => setSelectedSection(section)}
style={{
containerType: "inline-size",
containerName: containerName,
}}
// ? Apply container properties
className={twMerge(
"nwcb-w-full nwcb-relative nwcb-ring-2 nwcb-transition nwcb-duration-200 nwcb-ease-in-out",
selectedSection?.id === section.id
? "nwcb-ring-blue-400"
: "nwcb-ring-blue-400/20 hover:nwcb-ring-blue-400"
)}
style={{ opacity: 0.4 }}
className="nwcb-ring-2 nwcb-ring-blue-400 nwcb-w-full"
>
<style>{css}</style>

<SectionEditor
ref={
i ===
sections.findIndex(
(section) => section.id === selectedSection?.id
)
? selectedSectionRef
: undefined
}
key={`${section.id}`}
section={section}
index={i}
onSectionSelect={setSelectedSection}
onSwap={(section) => updateSection(i, section)}
onBlockSelect={setSelectedBlock}
section={sections.find((x) => x.id === activeId) as Section}
onSwap={() => {}}
onSectionSelect={() => {}}
onBlockSelect={() => {}}
index={0}
containerName={containerName}
/>
</div>
);
})}
</SortableContext>

<DragOverlay
// modifiers={[snapCenterToCursor]}
// dropAnimation={dropAnimation}
>
{activeId ? (
<div
style={{ opacity: 0.4 }}
className="nwcb-ring-2 nwcb-ring-blue-400 nwcb-w-full"
>
<SectionEditor
section={sections.find((x) => x.id === activeId) as Section}
onSwap={() => {}}
onSectionSelect={() => {}}
onBlockSelect={() => {}}
index={0}
containerName={containerName}
/>
</div>
) : null}
</DragOverlay>
</DndContext>
</form>

{children?.({
data: watch(),
selectedSection,
selectedBlock,
updateSelectedSection: updateSectionHandler,
updateSelectedBlock: updateBlockHandler,
addBlock: addBlockHandler,
})}
</FormProvider>
) : null}
</DragOverlay>
</DndContext>
</form>

{children}
</FormProvider>
</EditorContext.Provider>
);
};

Expand Down
42 changes: 0 additions & 42 deletions packages/ui/src/components/editor/EditorProvider.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EditorContextProps } from "@/types";
import { useState } from "react";
import { useEditorContext } from "../../Editor";
import Settings from "./Settings";

const SettingsButton = (props: EditorContextProps) => {
const SettingsButton = () => {
const ctx = useEditorContext();
const [toggle, setToggle] = useState<boolean>(false);
return (
<>
Expand All @@ -18,7 +19,7 @@ const SettingsButton = (props: EditorContextProps) => {
<div
className={`nwcb-fixed nwcb-end-0 nwcb-top-0 nwcb-transition nwcb-duration-150 nwcb-ease-in-out`}
>
<Settings {...props} />
<Settings {...ctx} />
</div>
)}
</>
Expand Down
Loading

0 comments on commit d344caf

Please sign in to comment.