Unverified Commit 27250721 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[segment explorer] redesign file pills and boundary trigger (#81302)

parent f0241781
......@@ -10,7 +10,6 @@
border-radius: 8px;
font-size: 14px;
line-height: 1.4;
min-width: 200px;
pointer-events: none;
}
......
import { forwardRef } from 'react'
import { forwardRef, useRef, useState } from 'react'
import { Tooltip as BaseTooltip } from '@base-ui-components/react/tooltip'
import { cx } from '../dev-overlay/utils/cx'
import './tooltip.css'
......@@ -7,22 +7,22 @@ type TooltipDirection = 'top' | 'bottom' | 'left' | 'right'
interface TooltipProps {
children: React.ReactNode
title: string
title: string | null
direction?: TooltipDirection
container?: HTMLElement | React.RefObject<HTMLElement>
arrowSize?: number
offset?: number
bgcolor?: string
color?: string
className?: string
}
export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
function Tooltip(
{
className,
children,
title,
direction = 'top',
container,
arrowSize = 6,
offset = 8,
bgcolor = '#000',
......@@ -30,9 +30,18 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
},
ref
) {
const [shadowRoot] = useState<ShadowRoot>(() => {
const ownerDocument = document
const portalNode = ownerDocument.querySelector('nextjs-portal')!
return portalNode.shadowRoot! as ShadowRoot
})
const shadowRootRef = useRef<ShadowRoot>(shadowRoot)
if (!title) {
return children
}
return (
<BaseTooltip.Provider>
<BaseTooltip.Root delay={0}>
<BaseTooltip.Root delay={400}>
<BaseTooltip.Trigger
ref={ref}
render={(triggerProps) => {
......@@ -40,7 +49,9 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
}}
/>
<BaseTooltip.Portal {...(container && { container })}>
{/* x-ref: https://github.com/mui/base-ui/issues/2224 */}
{/* @ts-expect-error remove this expect-error once shadowRoot is supported as container */}
<BaseTooltip.Portal container={shadowRootRef}>
<BaseTooltip.Positioner
side={direction}
sideOffset={offset + arrowSize}
......@@ -53,7 +64,7 @@ export const Tooltip = forwardRef<HTMLDivElement, TooltipProps>(
}
>
<BaseTooltip.Popup
className="tooltip"
className={cx('tooltip', className)}
style={
{
backgroundColor: bgcolor,
......
......@@ -84,14 +84,15 @@ export function useClickOutside(
rootRef: React.RefObject<HTMLElement | null>,
triggerRef: React.RefObject<HTMLButtonElement | null>,
active: boolean,
close: () => void
close: () => void,
ownerDocument?: Document
) {
useEffect(() => {
if (!active) {
return
}
const ownerDocument = rootRef.current?.ownerDocument
const ownerDocumentEl = ownerDocument || rootRef.current?.ownerDocument
function handleClickOutside(event: MouseEvent) {
const target = event.target as HTMLElement
......@@ -124,15 +125,15 @@ export function useClickOutside(
}
}
ownerDocument?.addEventListener('mousedown', handleClickOutside)
ownerDocument?.addEventListener('keydown', handleKeyDown)
ownerDocumentEl?.addEventListener('mousedown', handleClickOutside)
ownerDocumentEl?.addEventListener('keydown', handleKeyDown)
return () => {
ownerDocument?.removeEventListener('mousedown', handleClickOutside)
ownerDocument?.removeEventListener('keydown', handleKeyDown)
ownerDocumentEl?.removeEventListener('mousedown', handleClickOutside)
ownerDocumentEl?.removeEventListener('keydown', handleKeyDown)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [active])
}, [active, rootRef, triggerRef])
}
//////////////////////////////////////////////////////////////////////////////////////
......
.segment-boundary-trigger {
margin-left: auto;
gap: 8px;
}
.segment-boundary-trigger-button {
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
gap: 4px;
padding: 4px 6px;
line-height: 16px;
font-weight: 500;
color: var(--color-gray-1000);
border-radius: 6px;
background: transparent;
border-radius: 999px;
border: none;
font-size: var(--size-12);
cursor: pointer;
transition: background-color 0.15s ease;
}
.segment-boundary-trigger-text {
font-size: var(--size-12);
font-weight: 500;
user-select: none;
display: inline-flex;
align-items: center;
justify-content: center;
}
.segment-boundary-trigger-button svg {
width: 20px;
height: 20px;
.segment-boundary-trigger-text .plus-icon {
transition: transform 0.25s ease;
}
.segment-boundary-trigger-button:hover {
background: var(--color-gray-400);
color: var(--color-gray-1000);
.segment-boundary-trigger-text:hover .plus-icon {
color: var(--color-gray-800);
}
.segment-boundary-trigger svg {
width: 14px;
height: 14px;
flex-shrink: 0;
vertical-align: middle;
}
.segment-boundary-trigger:hover svg {
color: var(--color-gray-700);
}
.segment-boundary-trigger[disabled] svg,
.segment-boundary-trigger[disabled]:hover svg {
color: var(--color-gray-400);
cursor: not-allowed;
}
.segment-boundary-trigger--boundary {
background: var(--color-background-100);
box-shadow: inset 0 0 0 1px var(--color-gray-400);
}
.segment-boundary-trigger--boundary:hover {
background: var(--color-gray-200);
}
.segment-boundary-dropdown {
padding: 8px;
background: var(--color-background-100);
border: 1px solid var(--color-gray-400);
border-radius: 6px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 16px;
min-width: 120px;
user-select: none;
cursor: default;
box-shadow: 0px 4px 8px -4px color-mix(in srgb, var(--color-gray-900) 4%, transparent);
}
.segment-boundary-dropdown-positioner {
z-index: 2147483648;
z-index: var(--top-z-index);
}
.segment-boundary-dropdown-item {
display: flex;
align-items: center;
padding: 10px 8px;
padding: 8px;
line-height: 20px;
font-size: 14px;
border-radius: 6px;
......@@ -78,8 +110,10 @@
border-bottom-right-radius: 4px;
}
.segment-boundary-dropdown-divider {
height: 1px;
background: var(--color-gray-400);
margin: 8px 0;
.segment-boundary-group-label {
padding: 8px;
font-size: 13px;
line-height: 16px;
font-weight: 400;
color: var(--color-gray-900);
}
.segment-explorer-content {
font-size: var(--size-14);
padding: 0 8px;
width: 700px;
height: 400px;
max-width: calc(100vw - 40px);
max-height: calc(100vh - 240px);
}
.segment-explorer-page-route-bar {
......@@ -17,6 +21,7 @@
color: var(--color-gray-1000);
font-family: var(--font-mono);
white-space: nowrap;
line-height: 20px;
}
.segment-explorer-item {
......@@ -54,7 +59,7 @@
}
.segment-explorer-filename--path {
margin-right: 8px;
margin-right: auto;
}
.segment-explorer-filename--path small {
display: inline-block;
......@@ -68,57 +73,118 @@
.segment-explorer-files {
display: inline-flex;
gap: 8px;
margin-left: auto;
}
.segment-explorer-files + .segment-boundary-trigger {
margin-left: 8px;
}
.segment-explorer-file-label {
display: inline-flex;
align-items: center;
justify-content: center;
height: 20px;
padding: 2px 6px;
padding: 4px 6px;
border-radius: 16px;
line-height: 16px;
font-size: var(--size-12);
font-weight: 500;
user-select: none;
cursor: pointer;
}
.segment-explorer-file-label:hover {
filter: brightness(1.05);
}
.segment-explorer-file-label--layout,
.segment-explorer-file-label--template,
.segment-explorer-file-label--default {
background-color: var(--color-gray-300);
color: var(--color-gray-1000);
}
.segment-explorer-file-label--page {
background-color: var(--color-blue-300);
color: var(--color-blue-900);
}
.segment-explorer-file-label--not-found,
.segment-explorer-file-label--forbidden,
.segment-explorer-file-label--unauthorized {
.segment-explorer-file-label--overridden {
background-color: var(--color-amber-300);
color: var(--color-amber-900);
}
.segment-explorer-file-label--loading {
background-color: var(--color-green-300);
color: var(--color-green-900);
.segment-explorer-file-label .code-icon {
opacity: 0;
margin-left: 0;
width: 0;
transition: all 0.15s ease-in-out;
}
.segment-explorer-file-label--error,
.segment-explorer-file-label--global-error {
background-color: var(--color-red-300);
color: var(--color-red-900);
.segment-explorer-file-label:hover .code-icon {
opacity: 1;
width: 12px;
margin-left: 4px;
}
.segment-explorer-file-label:hover {
filter: brightness(0.95);
}
.segment-explorer-file-label--builtin {
background-color: transparent;
color: var(--color-gray-900);
border: 1px dashed var(--color-gray-500);
height: 24px;
cursor: default;
}
.segment-explorer-file-label--builtin svg {
margin-left: 4px;
margin-right: -4px;
}
/* Footer styles */
.segment-explorer-footer {
padding: 8px;
border-top: 1px solid var(--color-gray-400);
background-color: var(--color-background-100);
user-select: none;
}
.segment-explorer-footer-button {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
width: 100%;
padding: 6px;
background: var(--color-background-100);
border: 1px solid var(--color-gray-400);
border-radius: 6px;
color: var(--color-gray-1000);
font-size: var(--size-14);
font-weight: 500;
cursor: pointer;
transition: background-color 0.15s ease;
}
.segment-explorer-footer-button:hover:not(:disabled) {
background: var(--color-gray-200);
}
.segment-explorer-footer-button--disabled {
opacity: 0.5;
cursor: not-allowed;
}
.segment-explorer-footer-text {
text-align: center;
}
.segment-explorer-footer-badge {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 20px;
height: 20px;
padding: 0 6px;
background: var(--color-amber-300);
color: var(--color-amber-900);
border-radius: 10px;
font-size: var(--size-12);
font-weight: 600;
line-height: 1;
}
.segment-explorer-file-label-tooltip--sm {
white-space: nowrap;
}
.segment-explorer-file-label-tooltip--lg {
min-width: 200px;
}
......@@ -28,6 +28,7 @@ import type {
import { DEFAULT_SEGMENT_KEY } from '../../shared/lib/segment'
import {
BOUNDARY_PREFIX,
BOUNDARY_SUFFIX,
getConventionPathByType,
} from './segment-explorer-path'
......@@ -565,7 +566,7 @@ async function createComponentTreeInternal({
// Add a suffix to avoid conflict with the segment view node representing rendered file.
// existence: not-found.tsx@boundary
// rendered: not-found.tsx
const fileNameSuffix = '@boundary'
const fileNameSuffix = BOUNDARY_SUFFIX
const segmentViewBoundaries = isSegmentViewEnabled ? (
<>
{notFoundFilePath && (
......
import Link from 'next/link'
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div>
<h2>Layout - /no-layout/framework/blog</h2>
{children}
<Link href="/no-layout/framework">To /no-layout/framework</Link>
</div>
)
}
export default function NotFound() {
return <div>Not Found - /no-layout/framework/blog/not-found</div>
}
export default function Page() {
return <div>Page - /no-layout/framework/blog</div>
}
import Link from 'next/link'
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div>
<h1>Layout - /no-layout/framework</h1>
{children}
<Link href="/no-layout">To /no-layout</Link>
</div>
)
}
export default function NotFound() {
return <div>Not Found - /no-layout</div>
}
'use client'
export default function Error() {
return <div>Error @bar</div>
}
......@@ -206,4 +206,18 @@ describe('segment-explorer', () => {
default.tsx"
`)
})
it('should display boundary selector when a segment has only boundary files', async () => {
const browser = await next.browser('/no-layout/framework/blog')
expect(await getSegmentExplorerContent(browser)).toMatchInlineSnapshot(`
"app/
layout.tsx
no-layout/
framework/
layout.tsx
blog/
layout.tsx
page.tsx"
`)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment