-
Notifications
You must be signed in to change notification settings - Fork 10
feat(layout): add layout component #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SonyLeo
wants to merge
6
commits into
opentiny:develop
Choose a base branch
from
SonyLeo:feat/layout-code
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ddebbdd
feat(layout): introduce layout components with aside and floating states
SonyLeo 6170efc
refactor(layout): remove useLayoutPanel, introduce useLayoutProxyScro…
SonyLeo 9562068
feat: review suggestion
SonyLeo 69d167a
refactor(layout): update background colors and resize handles for lay…
SonyLeo bb231bf
refactor(layout): remove unused composables and utilities, streamline…
SonyLeo 46e968d
refactor(layout): enhance drawer visibility logic with slot content c…
SonyLeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| <script setup lang="ts"> | ||
| import { onKeyDown } from '@vueuse/core' | ||
| import { computed, ref, useAttrs } from 'vue' | ||
| import AsideContent from './components/AsideContent.vue' | ||
| import LayoutSurface from './components/LayoutSurface.vue' | ||
| import { provideLayoutContext } from './composables/useLayoutContext' | ||
| import { createLayoutState } from './composables/useLayoutRootState' | ||
| import type { LayoutAsideResizeDetail, LayoutEmits, LayoutProps, LayoutSlots } from './index.type' | ||
| import type { LayoutPanel } from './internal.type' | ||
| import { toPx } from './utils/cssLength' | ||
| import { emitAsideResizeEvent } from './utils/asideEventEmitters' | ||
| import { hasNonEmptySlotContent } from './utils/slots' | ||
|
|
||
| defineOptions({ | ||
| name: 'Layout', | ||
| inheritAttrs: false, | ||
| }) | ||
|
|
||
| const props = defineProps<LayoutProps>() | ||
| const emit = defineEmits<LayoutEmits>() | ||
| const attrs = useAttrs() | ||
| const slots = defineSlots<LayoutSlots>() | ||
|
|
||
| const { leftPanel, rightPanel, floating } = createLayoutState(props, emit) | ||
|
|
||
| function setDrawerOpen(panel: LayoutPanel, sibling: LayoutPanel, nextOpen: boolean): void { | ||
| if (nextOpen && panel.isDrawer.value && sibling.isDrawer.value && sibling.isOpen.value) { | ||
| sibling.setOpen(false) | ||
| } | ||
|
|
||
| panel.setOpen(nextOpen) | ||
| } | ||
|
|
||
| function toggleDrawer(panel: LayoutPanel, sibling: LayoutPanel): void { | ||
| setDrawerOpen(panel, sibling, !panel.isOpen.value) | ||
| } | ||
|
|
||
| function toggleLeftDrawer(): void { | ||
| toggleDrawer(leftPanel, rightPanel) | ||
| } | ||
|
|
||
| function toggleRightDrawer(): void { | ||
| toggleDrawer(rightPanel, leftPanel) | ||
| } | ||
|
|
||
| const isDrawerVisible = computed( | ||
| () => (leftPanel.isDrawer.value && leftPanel.isOpen.value) || (rightPanel.isDrawer.value && rightPanel.isOpen.value), | ||
| ) | ||
|
|
||
| function closeDrawers(): void { | ||
| if (leftPanel.isDrawer.value && leftPanel.isOpen.value) { | ||
| leftPanel.setOpen(false) | ||
| } | ||
|
|
||
| if (rightPanel.isDrawer.value && rightPanel.isOpen.value) { | ||
| rightPanel.setOpen(false) | ||
| } | ||
| } | ||
|
|
||
| const drawer = { | ||
| left: leftPanel, | ||
| right: rightPanel, | ||
| isDrawerVisible, | ||
| closeDrawers, | ||
| } | ||
|
|
||
| provideLayoutContext({ | ||
| left: { | ||
| isOpen: leftPanel.isOpen, | ||
| toggle: toggleLeftDrawer, | ||
| }, | ||
| right: { | ||
| isOpen: rightPanel.isOpen, | ||
| toggle: toggleRightDrawer, | ||
| }, | ||
| }) | ||
|
|
||
| const isAsideResizing = ref(false) | ||
|
|
||
| function onAsideResizeStart(detail: LayoutAsideResizeDetail): void { | ||
| isAsideResizing.value = true | ||
| emitAsideResizeEvent(emit, 'start', detail) | ||
| } | ||
|
|
||
| function onAsideResize(detail: LayoutAsideResizeDetail): void { | ||
| emitAsideResizeEvent(emit, 'progress', detail) | ||
| } | ||
|
|
||
| function onAsideResizeEnd(detail: LayoutAsideResizeDetail): void { | ||
| isAsideResizing.value = false | ||
| emitAsideResizeEvent(emit, 'end', detail) | ||
| } | ||
|
|
||
| function setLeftAsideWidth(width: number): void { | ||
| drawer.left.setWidth(width) | ||
| } | ||
|
|
||
| function setRightAsideWidth(width: number): void { | ||
| drawer.right.setWidth(width) | ||
| } | ||
|
|
||
| function getDockedAsideWidth(panel: LayoutPanel): number { | ||
| if (!panel.isDock.value || panel.isHidden.value) { | ||
| return 0 | ||
| } | ||
|
|
||
| return panel.isRail.value ? panel.collapsedWidth.value : panel.width.value | ||
| } | ||
|
|
||
| const leftDockWidth = computed(() => getDockedAsideWidth(drawer.left)) | ||
| const rightDockWidth = computed(() => getDockedAsideWidth(drawer.right)) | ||
|
|
||
| const hasLeftAside = computed(() => hasNonEmptySlotContent(slots['left-aside'])) | ||
| const hasHeader = computed(() => hasNonEmptySlotContent(slots.header)) | ||
| const hasFooter = computed(() => hasNonEmptySlotContent(slots.footer)) | ||
| const hasRightAside = computed(() => hasNonEmptySlotContent(slots['right-aside'])) | ||
|
|
||
| const layoutStyle = computed<Record<string, string>>(() => { | ||
| const style: Record<string, string> = {} | ||
| const leftDockWidth = toPx(drawer.left.width.value) | ||
| const leftCollapsedWidth = toPx(drawer.left.collapsedWidth.value) | ||
| const rightDockWidth = toPx(drawer.right.width.value) | ||
| const rightCollapsedWidth = toPx(drawer.right.collapsedWidth.value) | ||
|
|
||
| if (leftDockWidth) { | ||
| style['--left-dock-width'] = leftDockWidth | ||
| } | ||
|
|
||
| if (leftCollapsedWidth) { | ||
| style['--left-collapsed-width'] = leftCollapsedWidth | ||
| } | ||
|
|
||
| if (rightDockWidth) { | ||
| style['--right-dock-width'] = rightDockWidth | ||
| } | ||
|
|
||
| if (rightCollapsedWidth) { | ||
| style['--right-collapsed-width'] = rightCollapsedWidth | ||
| } | ||
|
|
||
| return style | ||
| }) | ||
|
|
||
| const layoutClass = computed(() => ({ | ||
| 'tr-layout--left-dock': hasLeftAside.value && drawer.left.isDock.value, | ||
| 'tr-layout--left-drawer': hasLeftAside.value && drawer.left.isDrawer.value, | ||
| 'tr-layout--left-expanded': hasLeftAside.value && drawer.left.isOpen.value, | ||
| 'tr-layout--left-rail': hasLeftAside.value && drawer.left.isRail.value, | ||
| 'tr-layout--right-dock': hasRightAside.value && drawer.right.isDock.value, | ||
| 'tr-layout--right-drawer': hasRightAside.value && drawer.right.isDrawer.value, | ||
| 'tr-layout--right-expanded': hasRightAside.value && drawer.right.isOpen.value, | ||
| 'tr-layout--right-rail': hasRightAside.value && drawer.right.isRail.value, | ||
| 'tr-layout--resizing': isAsideResizing.value, | ||
| })) | ||
|
|
||
| const layoutMode = floating.state.mode | ||
| const floatingStateValue = floating.state.value | ||
| const floatingValue = floating.state.resolved | ||
|
|
||
| onKeyDown('Escape', (event) => { | ||
| if (event.defaultPrevented || !drawer.isDrawerVisible.value) { | ||
| return | ||
| } | ||
|
|
||
| event.preventDefault() | ||
| event.stopPropagation() | ||
| drawer.closeDrawers() | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <LayoutSurface | ||
| v-bind="attrs" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不需要 |
||
| :mode="layoutMode" | ||
| :floating-state="floatingStateValue" | ||
| :resolved-floating="floatingValue" | ||
| :surface-class="layoutClass" | ||
| :surface-style="layoutStyle" | ||
| @floating-state-initialize="floating.actions.initialize" | ||
| @floating-state-change="floating.actions.commit" | ||
| @floating-drag-start="emit('floating-drag-start', $event)" | ||
| @floating-drag="emit('floating-drag', $event)" | ||
| @floating-drag-end="emit('floating-drag-end', $event)" | ||
| @floating-resize-start="emit('floating-resize-start', $event)" | ||
| @floating-resize="emit('floating-resize', $event)" | ||
| @floating-resize-end="emit('floating-resize-end', $event)" | ||
| > | ||
| <div class="tr-layout__body"> | ||
| <AsideContent | ||
| v-if="hasLeftAside" | ||
| side="left" | ||
| :opposite-dock-width="rightDockWidth" | ||
| :collapse-effect="drawer.left.collapseEffect.value" | ||
| :is-dock="drawer.left.isDock.value" | ||
| :is-drawer="drawer.left.isDrawer.value" | ||
| :is-open="drawer.left.isOpen.value" | ||
| :is-rail="drawer.left.isRail.value" | ||
| :is-hidden="drawer.left.isHidden.value" | ||
| :can-resize="drawer.left.canResize.value" | ||
| :min-width="drawer.left.minWidth.value" | ||
| :max-width="drawer.left.maxWidth.value" | ||
| @width-change="setLeftAsideWidth" | ||
| @aside-resize-start="onAsideResizeStart" | ||
| @aside-resize="onAsideResize" | ||
| @aside-resize-end="onAsideResizeEnd" | ||
| > | ||
| <slot name="left-aside" /> | ||
| </AsideContent> | ||
|
|
||
| <header v-if="hasHeader" class="tr-layout__header"> | ||
| <slot name="header" /> | ||
| </header> | ||
|
|
||
| <main class="tr-layout__main"> | ||
| <slot name="main" /> | ||
| </main> | ||
|
|
||
| <footer v-if="hasFooter" class="tr-layout__footer"> | ||
| <slot name="footer" /> | ||
| </footer> | ||
|
|
||
| <AsideContent | ||
| v-if="hasRightAside" | ||
| side="right" | ||
| :opposite-dock-width="leftDockWidth" | ||
| :collapse-effect="drawer.right.collapseEffect.value" | ||
| :is-dock="drawer.right.isDock.value" | ||
| :is-drawer="drawer.right.isDrawer.value" | ||
| :is-open="drawer.right.isOpen.value" | ||
| :is-rail="drawer.right.isRail.value" | ||
| :is-hidden="drawer.right.isHidden.value" | ||
| :can-resize="drawer.right.canResize.value" | ||
| :min-width="drawer.right.minWidth.value" | ||
| :max-width="drawer.right.maxWidth.value" | ||
| @width-change="setRightAsideWidth" | ||
| @aside-resize-start="onAsideResizeStart" | ||
| @aside-resize="onAsideResize" | ||
| @aside-resize-end="onAsideResizeEnd" | ||
| > | ||
| <slot name="right-aside" /> | ||
| </AsideContent> | ||
|
|
||
| <div | ||
| v-if="drawer.isDrawerVisible.value" | ||
| class="tr-layout__backdrop" | ||
| aria-hidden="true" | ||
| @pointerdown="drawer.closeDrawers" | ||
| /> | ||
| </div> | ||
| </LayoutSurface> | ||
| </template> | ||
|
|
||
| <style lang="less" scoped> | ||
| .tr-layout__body, | ||
| .tr-layout__header, | ||
| .tr-layout__main, | ||
| .tr-layout__footer { | ||
| min-width: 0; | ||
| min-height: 0; | ||
| } | ||
|
|
||
| .tr-layout__body { | ||
| position: relative; | ||
| display: grid; | ||
| box-sizing: border-box; | ||
| width: 100%; | ||
| height: 100%; | ||
| grid-template-columns: | ||
| var(--left-width) | ||
| minmax(var(--tr-layout-main-min-width, 320px), 1fr) | ||
| var(--right-width); | ||
| grid-template-rows: auto minmax(0, 1fr) auto; | ||
| grid-template-areas: | ||
| 'left header right' | ||
| 'left main right' | ||
| 'left footer right'; | ||
| overflow: hidden; | ||
| background: var(--tr-layout-bg); | ||
| border-radius: inherit; | ||
| transition: var( | ||
| --tr-layout-body-transition, | ||
| grid-template-columns var(--transition-duration) var(--transition-easing) | ||
| ); | ||
| } | ||
|
|
||
| .tr-layout__header { | ||
| grid-area: header; | ||
| background: var(--tr-layout-header-bg); | ||
| } | ||
|
|
||
| .tr-layout__main { | ||
| grid-area: main; | ||
| position: relative; | ||
| overflow: hidden; | ||
| background: var(--tr-layout-main-bg); | ||
| } | ||
|
|
||
| .tr-layout__footer { | ||
| grid-area: footer; | ||
| background: var(--tr-layout-footer-bg); | ||
| } | ||
|
|
||
| .tr-layout__backdrop { | ||
| position: absolute; | ||
| inset: 0; | ||
| z-index: calc(var(--overlay-z-index) - 1); | ||
| background: var(--tr-layout-overlay-bg); | ||
| cursor: pointer; | ||
| } | ||
| </style> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.