Skip to content

Commit

Permalink
v1.4.0 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlemTuzlak authored Jul 28, 2023
1 parent 6479bb6 commit 9018a16
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 98 deletions.
93 changes: 92 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,101 @@ The `RemixDevTools` component accepts the following props:
- `defaultOpen`: Whether to open the Remix Development Tools by default. Defaults to `false`.
- `position`: The position of the Remix Development Tools trigger. Defaults to `bottom-right`.
- `requireUrlFlag`: Requires rdt=true to be present in the URL search to open the Remix Development Tools. Defaults to `false`.
- `showRouteBoundaries`: Allows you to see each Outlet and route boundaries by coloring the background. Defaults to `false`.
- [**DEPRECATED**] `showRouteBoundaries`:This flag has been deprecated in favor of adding route boundaries. Please see the section below for more information.
- `hideUntilHover`: Allows you to hide the trigger until you hover over it. Defaults to `false`.
- `additionalTabs`: Allows you to provide additional tabs to the Remix Development Tools. Defaults to `[]`.

## Adding route boundaries

The `showErrorBoundaries` flag has been deprecated in favor of this method. Please use it instead.

In order to add Route boundaries to your project you need to do the following two things:

1. Modify your `entry.server.ts` to add the following code:

```diff
function handleBrowserRequest(
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext
) {
- return new Promise((resolve, reject) => {
+ return new Promise(async (resolve, reject) => {
let shellRendered = false;
+ const context = process.env.NODE_ENV === "development" ? await import("remix-development-tools").then(({ initRouteBoundariesServer }) => initRouteBoundariesServer(remixContext)) : remixContext;
const { pipe, abort } = renderToPipeableStream(
<RemixServer
- context={remixContext}
+ context={context}
url={request.url}
abortDelay={ABORT_DELAY}
/>,
{
onShellReady() {
shellRendered = true;
const body = new PassThrough();

responseHeaders.set("Content-Type", "text/html");

resolve(
new Response(body, {
headers: responseHeaders,
status: responseStatusCode,
})
);

pipe(body);
},
onShellError(error: unknown) {
reject(error);
},
onError(error: unknown) {
responseStatusCode = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
}
);

setTimeout(abort, ABORT_DELAY);
});
}
```

2. Modify your `entry.client.tsx` to add the following code:

```diff
+ if(process.env.NODE_ENV === "development") {
+ import("remix-development-tools").then(({ initRouteBoundariesClient }) => {
+ initRouteBoundariesClient();
+ startTransition(() => {
+ hydrateRoot(
+ document,
+ <StrictMode>
+ <RemixBrowser />
+ </StrictMode>
+ );
+ });
+
+ });
+ } else {
startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<RemixBrowser />
</StrictMode>
);
});
+ }
```
3. You are good to go. Now you can see the route boundaries in your project when you hover each route.

## Plugins

Writing plugins for Remix Development Tools is easy. You can write a plugin that adds a new tab to the Remix Development Tools in the following way:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "remix-development-tools",
"description": "Remix development tools.",
"author": "Alem Tuzlak",
"version": "1.3.0",
"version": "1.4.0",
"license": "MIT",
"keywords": [
"remix",
Expand Down
12 changes: 2 additions & 10 deletions src/RemixDevTools/RemixDevTools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useTimelineHandler } from "./hooks/useTimelineHandler";
import { useRDTContext } from "./context/useRDTContext";
import { isDev } from "./utils/isDev";
import { useLocation } from "@remix-run/react";
import { useOutletAugment } from "./hooks/useOutletAugment";
import { Trigger } from "./components/Trigger";
import { MainPanel } from "./layout/MainPanel";
import { Tabs } from "./layout/Tabs";
Expand All @@ -15,17 +14,14 @@ interface Props extends RemixDevToolsProps {
defaultOpen: boolean;
position: Exclude<RemixDevToolsProps["position"], undefined>;
hideUntilHover: boolean;
showRouteBoundaries: boolean;
}

const RemixDevTools = ({
defaultOpen,
position,
additionalTabs,
hideUntilHover,
showRouteBoundaries,
}: Props) => {
useOutletAugment(showRouteBoundaries);
useTimelineHandler();
const { persistOpen } = useRDTContext();
const [isOpen, setIsOpen] = useState(defaultOpen || persistOpen);
Expand All @@ -49,7 +45,6 @@ const RemixDevTools = ({
</div>
);
};

let hydrating = true;

function useHydrated() {
Expand Down Expand Up @@ -78,8 +73,6 @@ export interface RemixDevToolsProps {
| "top-left"
| "middle-right"
| "middle-left";
// Show route boundaries when you hover over a route in active page tab
showRouteBoundaries?: boolean;
// Additional tabs to add to the dev tools
additionalTabs?: Tab[];
// Whether the dev tools trigger should hide until hovered
Expand All @@ -90,7 +83,6 @@ const RDTWithContext = ({
port = 3003,
defaultOpen = false,
requireUrlFlag,
showRouteBoundaries = false,
position = "bottom-right",
hideUntilHover = false,
additionalTabs,
Expand All @@ -101,14 +93,14 @@ const RDTWithContext = ({

if (!hydrated || !isDevelopment) return null;
if (requireUrlFlag && !url.includes("rdt=true")) return null;

return (
<RDTContextProvider showRouteBoundaries={showRouteBoundaries} port={port}>
<RDTContextProvider port={port}>
<RemixDevTools
defaultOpen={defaultOpen}
position={position}
additionalTabs={additionalTabs}
hideUntilHover={hideUntilHover}
showRouteBoundaries={showRouteBoundaries}
/>
</RDTContextProvider>
);
Expand Down
7 changes: 1 addition & 6 deletions src/RemixDevTools/context/RDTContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ interface ContextProps {

export const REMIX_DEV_TOOLS = "remixDevTools";

export const RDTContextProvider = ({
children,
port,
showRouteBoundaries,
}: ContextProps) => {
export const RDTContextProvider = ({ children, port }: ContextProps) => {
const existingState = sessionStorage.getItem(REMIX_DEV_TOOLS);
const settings = localStorage.getItem(REMIX_DEV_TOOLS);

const [state, dispatch] = useReducer(rdtReducer, {
...initialState,
...(existingState ? JSON.parse(existingState) : {}),
showRouteBoundaries,
settings: settings
? { ...initialState.settings, ...JSON.parse(settings), port }
: { ...initialState.settings, port },
Expand Down
2 changes: 0 additions & 2 deletions src/RemixDevTools/context/rdtReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type RouteWildcards = Record<string, Record<string, string> | undefined>;

export type RemixDevToolsState = {
timeline: TimelineEvent[];
showRouteBoundaries?: boolean;
terminals: Terminal[];
settings: {
routeWildcards: RouteWildcards;
Expand All @@ -20,7 +19,6 @@ export type RemixDevToolsState = {

export const initialState: RemixDevToolsState = {
timeline: [],
showRouteBoundaries: false,
terminals: [{ id: 0, locked: false, output: [], history: [] }],
settings: {
routeWildcards: {},
Expand Down
4 changes: 1 addition & 3 deletions src/RemixDevTools/context/useRDTContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const useRDTContext = () => {
throw new Error("useRDTContext must be used within a RDTContextProvider");
}
const { state, dispatch } = context;
const { timeline, settings, showRouteBoundaries, terminals, persistOpen } =
state;
const { timeline, settings, terminals, persistOpen } = state;
const { activeTab, shouldConnectWithForge, routeWildcards, port, height } =
settings;

Expand Down Expand Up @@ -140,7 +139,6 @@ const useRDTContext = () => {
routeWildcards,
port,
height,
showRouteBoundaries,
setHeight,
setRouteWildcards,
terminals,
Expand Down
53 changes: 0 additions & 53 deletions src/RemixDevTools/hooks/useOutletAugment.tsx

This file was deleted.

4 changes: 4 additions & 0 deletions src/RemixDevTools/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export { RemixDevTools } from "./RemixDevTools";
export { useRemixForgeSocketExternal as useRemixForgeSocket } from "./hooks/useRemixForgeSocket";
export {
initRouteBoundariesClient,
initRouteBoundariesServer,
} from "./methods/boundaries";
64 changes: 64 additions & 0 deletions src/RemixDevTools/methods/boundaries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { EntryContext } from "@remix-run/server-runtime";
import clsx from "clsx";

export const initRouteBoundariesServer = (context: EntryContext) => {
return {
...context,
routeModules: Object.entries(context.routeModules).reduce(
(acc, [key, value]) => {
if (key === "root") {
return { ...acc, [key]: value };
}
return {
...acc,
[key]: {
...value,
default: () => {
return (
<>
<div
className={clsx(
"rdt-invisible rdt-absolute rdt-hidden rdt-h-0 rdt-w-0",
key
)}
/>
<value.default />
</>
);
},
},
};
},
{}
),
};
};

export const initRouteBoundariesClient = () => {
window.__remixRouteModules = new Proxy(window.__remixRouteModules, {
get: function (target, property) {
if (property === "root") return target[property];
const value = target[property as any];
if (value?.default && value.default.name !== "hooked") {
return {
...value,
default: function hooked() {
return (
<>
<div
className={clsx(
"rdt-invisible rdt-absolute rdt-hidden rdt-h-0 rdt-w-0",
property as string
)}
/>
<value.default />
</>
);
},
};
}

return value;
},
});
};
3 changes: 0 additions & 3 deletions src/RemixDevTools/tabs/PageTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Tag } from "../components/Tag";
import { VsCodeButton } from "../components/VScodeButton";
import { useMemo } from "react";
import { isLayoutRoute } from "../utils/routing";
import { useRDTContext } from "../context/useRDTContext";

export const ROUTE_COLORS: Record<string, string> = {
ROUTE: "rdt-bg-green-500 rdt-text-white",
Expand Down Expand Up @@ -46,9 +45,7 @@ const PageTab = () => {
const reversed = useMemo(() => routes.reverse(), [routes]);
const { revalidate, state } = useRevalidator();
const { isConnected, sendJsonMessage } = useRemixForgeSocket();
const { showRouteBoundaries } = useRDTContext();
const onHover = (path: string, type: "enter" | "leave") => {
if (!showRouteBoundaries) return;
const classes =
"rdt-bg-green-100 rdt-transition-all rdt-rounded rdt-apply-tw rdt-bg-gradient-to-r rdt-from-cyan-500/50 rdt-to-blue-500/50";
const isRoot = path === "root";
Expand Down
Loading

0 comments on commit 9018a16

Please sign in to comment.