Skip to content

Commit a67b168

Browse files
authored
Replace tunnel-rat with a portal (#3444)
[tunnel-rat](https://github.com/pmndrs/tunnel-rat) is how pagination and full-page form buttons get into the sticky footer of `ContentPane`. It hasn't had a release since 2023 and it installs its own copy of zustand 4 next to our zustand 5. It was written to move elements between React renderers, which we don't need: we only have one renderer, so `createPortal` is sufficient. This PR replaces the tunnel with a `PageFooter` component that portals its children into the footer element, which `ContentPane` registers in a small zustand store through a ref callback. Why it's better: - Footer content is rendered once, in place. tunnel-rat copied each source's children into a store in a layout effect on every render of the page and rendered them again from the store. - One fewer dependency, no second zustand, and less code. Potential issues and why they don't matter: - tunnel-rat kept multiple sources in a defined order. Portals append in mount order. But we don't care because only one thing is ever in the footer at a time: `FullPageForm` is the only user of page actions, and no full-page form has a paginated table. - React events from footer content now bubble through the page's tree before reaching `ContentPane`. No page has a handler on an element around its table or form buttons, so nothing changes. Added an e2e test that navigates from the projects list into a project, to the instance create form, and back, checking that the footer shows pagination, then the form buttons, then pagination again.
1 parent e9c1e3c commit a67b168

9 files changed

Lines changed: 63 additions & 65 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
# Layout & accessibility
116116

117117
- Build pages inside the shared `PageContainer`/`ContentPane` (`app/layouts/helpers.tsx`).
118-
- Surface page-level buttons and pagination via the `PageActions` and `Pagination` tunnels from `tunnel-rat`; anything rendered through `.In` lands in `.Target` automatically.
118+
- To put page-level buttons or pagination in the sticky footer at the bottom of `ContentPane`, wrap them in `PageFooter` (`app/components/PageFooter.tsx`), which portals its children there. `FullPageForm` actions and the `Pagination` component already do this.
119119
- For global loading states, reuse `PageSkeleton`—it keeps the MSW banner and grid layout stable, and `skipPaths` lets you opt-out for routes with custom layouts (`app/components/PageSkeleton.tsx`).
120120
- Enforce accessibility at the type level: use `AriaLabel` type from `app/ui/util/aria.ts` which requires exactly one of `aria-label` or `aria-labelledby` on custom interactive components.
121121

‎app/components/PageActions.tsx‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎app/components/PageFooter.tsx‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import type { ReactNode } from 'react'
9+
import { createPortal } from 'react-dom'
10+
import { create } from 'zustand'
11+
12+
/** The sticky footer element at the bottom of `ContentPane` */
13+
const useFooter = create<{ el: HTMLElement | null }>(() => ({ el: null }))
14+
15+
/** Ref callback for the footer element */
16+
export const setPageFooter = (el: HTMLElement | null) => useFooter.setState({ el })
17+
18+
/**
19+
* Render page-level buttons or pagination in the sticky footer at the bottom
20+
* of the content pane, outside the page's scrolling content.
21+
*/
22+
export function PageFooter({ children }: { children: ReactNode }) {
23+
const footer = useFooter((s) => s.el)
24+
// null on the first render of a ContentPane, before the footer has mounted
25+
return footer && createPortal(children, footer)
26+
}

‎app/components/Pagination.tsx‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import tunnel from 'tunnel-rat'
9-
108
import {
119
Pagination as UIPagination,
1210
type PaginationProps as UIPaginationProps,
1311
} from '~/ui/lib/Pagination'
1412

15-
const Tunnel = tunnel()
13+
import { PageFooter } from './PageFooter'
1614

1715
export function Pagination(props: UIPaginationProps) {
1816
return (
19-
<Tunnel.In>
17+
<PageFooter>
2018
<UIPagination className="gutter h-14 py-5" {...props} />
21-
</Tunnel.In>
19+
</PageFooter>
2220
)
2321
}
24-
25-
Pagination.Target = Tunnel.Out

‎app/components/form/FullPageForm.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { flattenChildren, pluckFirstOfType } from '~/util/children'
1616
import { classed } from '~/util/classed'
1717

1818
import { Form } from '../form/Form'
19-
import { PageActions } from '../PageActions'
19+
import { PageFooter } from '../PageFooter'
2020

2121
interface FullPageFormProps<TFieldValues extends FieldValues> {
2222
id: string
@@ -106,7 +106,7 @@ export function FullPageForm<TFieldValues extends FieldValues>({
106106
{!isSubmitSuccessful && <ConfirmNavigation blocker={blocker} />}
107107

108108
{actions && (
109-
<PageActions>
109+
<PageFooter>
110110
<PageActionsContainer>
111111
{cloneElement(actions, {
112112
formId: id,
@@ -115,7 +115,7 @@ export function FullPageForm<TFieldValues extends FieldValues>({
115115
error: error || submitError,
116116
})}
117117
</PageActionsContainer>
118-
</PageActions>
118+
</PageFooter>
119119
)}
120120
</>
121121
)

‎app/layouts/helpers.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
*/
88
import { Outlet } from 'react-router'
99

10-
import { PageActionsTarget } from '~/components/PageActions'
11-
import { Pagination } from '~/components/Pagination'
10+
import { setPageFooter } from '~/components/PageFooter'
1211
import { useScrollRestoration } from '~/hooks/use-scroll-restoration'
1312
import { classed } from '~/util/classed'
1413

@@ -38,10 +37,11 @@ export function ContentPane() {
3837
<Outlet />
3938
</main>
4039
</div>
41-
<div className="bg-default border-secondary sticky bottom-0 z-(--z-top-bar) shrink-0 justify-between overflow-hidden border-t empty:border-t-0">
42-
<Pagination.Target />
43-
<PageActionsTarget />
44-
</div>
40+
{/* pagination and form actions portal in here through PageFooter */}
41+
<div
42+
ref={setPageFooter}
43+
className="bg-default border-secondary sticky bottom-0 z-(--z-top-bar) shrink-0 justify-between overflow-hidden border-t empty:border-t-0"
44+
/>
4545
</div>
4646
)
4747
}

‎package-lock.json‎

Lines changed: 0 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
"shiki": "^4.5.0",
8585
"simplebar-react": "^3.2.6",
8686
"ts-pattern": "^5.8.0",
87-
"tunnel-rat": "^0.1.2",
8887
"uplot": "^1.6.32",
8988
"uplot-react": "^1.2.4",
9089
"use-debounce": "^10.0.4",

‎test/e2e/pagination.e2e.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,27 @@ test('pagination', async ({ page }) => {
6969
await nextButton.click()
7070
await expect(spinner).toBeHidden({ timeout: 10 }) // no spinner, cached page
7171
})
72+
73+
// Pagination and full-page form buttons are portaled into the same footer
74+
// element, which is replaced when the layout changes
75+
test('footer content follows navigation', async ({ page }) => {
76+
const pagination = page.getByRole('navigation', { name: 'Pagination' })
77+
const submit = page.getByRole('button', { name: 'Create instance' })
78+
79+
await page.goto('/projects')
80+
await expect(pagination).toBeVisible()
81+
82+
// silo layout to project layout
83+
await page.getByRole('link', { name: 'mock-project' }).click()
84+
await expect(page.getByRole('heading', { name: 'Instances' })).toBeVisible()
85+
await expect(pagination).toBeVisible()
86+
87+
await page.getByRole('link', { name: 'New Instance' }).click()
88+
await expect(submit).toBeVisible()
89+
await expect(pagination).toBeHidden()
90+
91+
await page.getByRole('button', { name: 'Cancel' }).click()
92+
await expect(page.getByRole('heading', { name: 'Instances' })).toBeVisible()
93+
await expect(pagination).toBeVisible()
94+
await expect(submit).toBeHidden()
95+
})

0 commit comments

Comments
 (0)