Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion resources/boost/skills/saucebase-module-development/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ Navigation::add('Roadmap', fn () => route('roadmap.index'), function (Section $s
Pass the URL as a closure: it resolves when navigation renders, not when the file loads. `Navigation::addWhen($condition, ...)`
shows an item only while `$condition` returns true. Besides `group`, `slug`, `icon`, and `order`, items may
set `action`, `type`, `external`, `newPage`, `class`, and `badge`. The `icon` and `action` names must be
registered by the frontend; see `saucebase-frontend-development`.
registered by the frontend; see `saucebase-frontend-development`. An item is active on its exact URL; for a
section with pages under it, `active` decides instead (a bool or closure: `fn () => request()->routeIs('chat.*')`).

## Breadcrumbs

Expand Down
5 changes: 4 additions & 1 deletion src/Navigation/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ protected function transformItem(array $item): array

$menuItem = [
'title' => $item['title'],
'active' => $this->isItemActiveForUrl($resolvedUrl),
// An `active` attribute (bool or closure) decides for a section with pages under it.
'active' => array_key_exists('active', $attributes)
? (bool) value($attributes['active'])
: $this->isItemActiveForUrl($resolvedUrl),
'slug' => $attributes['slug'] ?? Str::slug($item['title'], '-'),
];

Expand Down
2 changes: 1 addition & 1 deletion stubs/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Built with [Saucebase](https://github.com/saucebase-dev/saucebase), on Laravel a

| Installed with | Open the app |
| --- | --- |
| Herd | Already served at `https://<folder>.test`. Run `npm run dev` for live reload. |
| Herd or Lerd | Herd serves `https://<folder>.test`. Lerd serves `http://<folder>.test`; `lerd secure <folder>` adds HTTPS. Run `npm run dev` for live reload. |
| Native | `composer dev` (app, queue, logs and Vite), then open the URL it prints. |
| Docker | `docker compose up -d`, then open `APP_URL` from `.env`. Logs: `docker compose logs -f`. Stop: `docker compose down`. |

Expand Down
108 changes: 108 additions & 0 deletions stubs/stack/react/config/package-lock.json

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

1 change: 1 addition & 0 deletions stubs/stack/react/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"prettier": "^3.9.10",
"prettier-plugin-organize-imports": "^4.3.0",
"prettier-plugin-tailwindcss": "^0.8.1",
"react-docgen": "^8.0.4",
"shadcn": "^4.22.1",
"storybook": "^11.0.0-rc.0",
"storybook-addon-pseudo-states": "^11.0.0-rc.0",
Expand Down
82 changes: 82 additions & 0 deletions stubs/stack/react/js/components/SidebarResizer.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import preview from '@/.storybook/preview';
import { useState, type CSSProperties } from 'react';
import { expect, waitFor } from 'storybook/test';
import { SidebarResizer } from './SidebarResizer';

/**
* A sidebar's draggable edge: drag it, or use the arrow keys, between 224 and 448px; a
* double click resets it. AppLayout renders it for the app sidebar (`resizable`), where it
* is fixed to the window. `contained` puts it on a panel inside the page instead: it sits
* on the panel's edge and measures from the panel's left.
*/
const meta = preview.meta({
title: 'App/SidebarResizer',
component: SidebarResizer,
parameters: { layout: 'fullscreen' },
});
export default meta;

function Panel() {
const [width, setWidth] = useState<number | null>(null);
const [resizing, setResizing] = useState(false);

return (
<div
className="relative ml-[200px] h-72"
style={
{
'--sidebar-width': width ? `${width}px` : '16rem',
} as CSSProperties
}
>
<div
className="bg-muted h-full w-(--sidebar-width) rounded-md"
data-testid="panel"
/>
<SidebarResizer
width={width}
onWidthChange={setWidth}
resizing={resizing}
onResizingChange={setResizing}
contained
/>
</div>
);
}

/** On a panel 200px in from the window's edge: the drag sets the panel's own width. */
export const Contained = meta.story({
render: () => <Panel />,
play: async ({ canvas, userEvent }) => {
const handle = canvas.getByRole('separator', {
name: 'Resize sidebar',
});
const panel = canvas.getByTestId('panel');
const start = panel.getBoundingClientRect();
const box = handle.getBoundingClientRect();

// Positioned in the panel: a fixed handle is placed by the window, measured from its left.
await expect(getComputedStyle(handle).position).toBe('absolute');
// On the panel's edge, not 256px from the window's.
await expect(
Math.abs(box.left + box.width / 2 - start.right),
).toBeLessThanOrEqual(2);

const y = box.top + box.height / 2;
await userEvent.pointer([
{
keys: '[MouseLeft>]',
target: handle,
coords: { clientX: start.right, clientY: y },
},
{
target: handle,
coords: { clientX: start.right + 80, clientY: y },
},
{ keys: '[/MouseLeft]', target: handle },
]);
await waitFor(() =>
expect(panel.getBoundingClientRect().width).toBe(336),
);
},
});
90 changes: 90 additions & 0 deletions stubs/stack/react/js/components/SidebarResizer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { useT } from '@saucebase/react/i18n';
import {
clampSidebarWidth as clamp,
SIDEBAR_DEFAULT_WIDTH,
SIDEBAR_MAX_WIDTH as MAX,
SIDEBAR_MIN_WIDTH as MIN,
} from '@/hooks/useSidebarState';
import { useRef } from 'react';

const STEP = 16;

interface SidebarResizerProps {
width: number | null;
/** On a panel inside the page (positioned, `relative`) rather than fixed to the window. */
contained?: boolean;
onWidthChange: (width: number | null) => void;
resizing: boolean;
onResizingChange: (resizing: boolean) => void;
}

/**
* The sidebar's edge: drag it, or use the arrow keys, to resize the sidebar; a double
* click resets it. Rendered right after the sidebar inside SidebarProvider, so it sits on
* `--sidebar-width` and hides with the collapsed sidebar (the sidebar is its `peer`).
* Left-side sidebars only: the width is the pointer's distance from the sidebar's left edge
* (the window's, or the positioned panel the handle sits in).
*/
export function SidebarResizer({
width,
onWidthChange,
resizing,
onResizingChange,
contained = false,
}: SidebarResizerProps) {
const t = useT();
const handle = useRef<HTMLDivElement>(null);

// The sidebar's left edge: the window's for the app sidebar (the handle is fixed, so it
// has no offsetParent), a panel's own when the handle sits in it (positioned, `relative`).
const origin = () =>
handle.current?.offsetParent?.getBoundingClientRect().left ?? 0;

const nudge = (by: number) => {
// The handle straddles the edge: its middle is the current width.
const rect = handle.current!.getBoundingClientRect();
onWidthChange(clamp(rect.left + rect.width / 2 - origin() + by));
};

// A focusable separator with a value is the WAI-ARIA window splitter: interactive by spec.
/* eslint-disable jsx-a11y/no-noninteractive-tabindex, jsx-a11y/no-noninteractive-element-interactions */
return (
<div
ref={handle}
role="separator"
aria-orientation="vertical"
aria-label={t('Resize sidebar')}
aria-valuenow={width ?? SIDEBAR_DEFAULT_WIDTH}
aria-valuemin={MIN}
aria-valuemax={MAX}
tabIndex={0}
data-testid="sidebar-resize"
className={`${contained ? 'absolute' : 'fixed'} after:bg-sidebar-border inset-y-0 left-[calc(var(--sidebar-width)-4px)] z-20 hidden w-2 cursor-col-resize touch-none outline-hidden after:absolute after:inset-y-0 after:left-1/2 after:w-0.5 after:-translate-x-1/2 after:opacity-0 after:transition-opacity hover:after:opacity-100 focus-visible:after:opacity-100 md:block peer-data-[state=collapsed]:hidden`}
onPointerDown={(event) => {
// The primary button only: a right-click's context menu can swallow pointerup.
if (event.button !== 0) {
return;
}

handle.current?.setPointerCapture(event.pointerId);
onResizingChange(true);
}}
onPointerMove={(event) => {
if (resizing) {
onWidthChange(clamp(event.clientX - origin()));
}
}}
onPointerUp={() => onResizingChange(false)}
onPointerCancel={() => onResizingChange(false)}
onLostPointerCapture={() => onResizingChange(false)}
onDoubleClick={() => onWidthChange(null)}
onKeyDown={(event) => {
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
event.preventDefault();
nudge(event.key === 'ArrowLeft' ? -STEP : STEP);
}
}}
/>

Check warning on line 87 in stubs/stack/react/js/components/SidebarResizer.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use <hr> instead of the "separator" role to ensure accessibility across all devices.

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_core&issues=AaIpKbiqhqSjRniphkaC&open=AaIpKbiqhqSjRniphkaC&pullRequest=47
);
/* eslint-enable jsx-a11y/no-noninteractive-tabindex, jsx-a11y/no-noninteractive-element-interactions */
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ interface NavGroupProps {
}

export default function NavGroup({ items, className }: NavGroupProps) {
// No links, no group: an empty one keeps its padding and pushes the next block down.
if (items.length === 0) {
return null;
}

return (
<SidebarGroup className={className}>
<SidebarMenu>
Expand Down
Loading