react-mosaic

react-mosaic

React平铺窗口管理组件

react-mosaic是一个React平铺窗口管理组件,用于灵活布局和管理复杂的用户界面。它支持窗格大小调整、拖放重排,并提供可自定义的工具栏。采用TypeScript开发,也兼容JavaScript。组件支持受控和非受控两种使用模式,适用于各类应用需求。react-mosaic通过简洁的API,实现了对工作空间的高度可配置化管理。

react-mosaic窗口管理器React组件可调整布局Blueprint主题Github开源项目

react-mosaic

CircleCI npm

react-mosaic is a full-featured React Tiling Window Manager meant to give a user complete control over their workspace. It provides a simple and flexible API to tile arbitrarily complex react components across a user's view. react-mosaic is written in TypeScript and provides typings but can be used in JavaScript as well.

The best way to see it is a simple Demo.

Screencast

screencast demo

Usage

The core of react-mosaic's operations revolve around the simple binary tree specified by MosaicNode<T>. T is the type of the leaves of the tree and is a string or a number that can be resolved to a JSX.Element for display.

Installation

  1. yarn add react-mosaic-component
  2. Make sure react-mosaic-component.css is included on your page.
  3. Import the Mosaic component and use it in your app.
  4. (Optional) Install Blueprint

Blueprint Theme

Without a theme, Mosaic only loads the styles necessary for it to function - making it easier for the consumer to style it to match their own app.

By default, Mosaic renders with the mosaic-blueprint-theme class. This uses the excellent Blueprint React UI Toolkit to provide a good starting state. It is recommended to at least start developing with this theme. To use it install Blueprint yarn add @blueprintjs/core @blueprintjs/icons and add their CSS to your page. Don't forget to set blueprintNamespace in Mosaic to the correct value for the version of Blueprint you are using.

See blueprint-theme.less for an example of creating a theme.

Blueprint Dark Theme

Mosaic supports the Blueprint Dark Theme out of the box when rendered with the mosaic-blueprint-theme bp3-dark class.

Examples

Simple Tiling

app.css
html, body, #app { height: 100%; width: 100%; margin: 0; }
App.tsx
import { Mosaic } from 'react-mosaic-component'; import 'react-mosaic-component/react-mosaic-component.css'; import '@blueprintjs/core/lib/css/blueprint.css'; import '@blueprintjs/icons/lib/css/blueprint-icons.css'; import './app.css'; const ELEMENT_MAP: { [viewId: string]: JSX.Element } = { a: <div>Left Window</div>, b: <div>Top Right Window</div>, c: <div>Bottom Right Window</div>, }; export const app = ( <div id="app"> <Mosaic<string> renderTile={(id) => ELEMENT_MAP[id]} initialValue={{ direction: 'row', first: 'a', second: { direction: 'column', first: 'b', second: 'c', }, splitPercentage: 40, }} /> </div> );

renderTile is a stateless lookup function to convert T into a displayable JSX.Element. By default T is string (so to render one element initialValue="ID" works). Ts must be unique within an instance of Mosaic, they are used as keys for React list management. initialValue is a MosaicNode<T>.

The user can resize these panes but there is no other advanced functionality. This example renders a simple tiled interface with one element on the left half, and two stacked elements on the right half. The user can resize these panes but there is no other advanced functionality.

Drag, Drop, and other advanced functionality with MosaicWindow

MosaicWindow is a component that renders a toolbar and controls around its children for a tile as well as providing full featured drag and drop functionality.

export type ViewId = 'a' | 'b' | 'c' | 'new'; const TITLE_MAP: Record<ViewId, string> = { a: 'Left Window', b: 'Top Right Window', c: 'Bottom Right Window', new: 'New Window', }; export const app = ( <Mosaic<ViewId> renderTile={(id, path) => ( <MosaicWindow<ViewId> path={path} createNode={() => 'new'} title={TITLE_MAP[id]}> <h1>{TITLE_MAP[id]}</h1> </MosaicWindow> )} initialValue={{ direction: 'row', first: 'a', second: { direction: 'column', first: 'b', second: 'c', }, }} /> );

Here T is a ViewId that can be used to look elements up in TITLE_MAP. This allows for easy view state specification and serialization. This will render a view that looks very similar to the previous examples, but now each of the windows will have a toolbar with buttons. These toolbars can be dragged around by a user to rearrange their workspace.

MosaicWindow API docs here.

Controlled vs. Uncontrolled

Mosaic views have two modes, similar to React.DOM input elements:

  • Controlled, where the consumer manages Mosaic's state through callbacks. Using this API, the consumer can perform any operation upon the tree to change the the view as desired.
  • Uncontrolled, where Mosaic manages all of its state internally.

See Controlled Components.

All of the previous examples show use of Mosaic in an Uncontrolled fashion.

Example Application

See ExampleApp (the application used in the Demo) for a more interesting example that shows the usage of Mosaic as a controlled component and modifications of the tree structure.

API

Mosaic Props

export interface MosaicBaseProps<T extends MosaicKey> { /** * Lookup function to convert `T` to a displayable `JSX.Element` */ renderTile: TileRenderer<T>; /** * Called when a user initiates any change to the tree (removing, adding, moving, resizing, etc.) */ onChange?: (newNode: MosaicNode<T> | null) => void; /** * Called when a user completes a change (fires like above except for the interpolation during resizing) */ onRelease?: (newNode: MosaicNode<T> | null) => void; /** * Additional classes to affix to the root element * Default: 'mosaic-blueprint-theme' */ className?: string; /** * Options that control resizing * @see: [[ResizeOptions]] */ resize?: ResizeOptions; /** * View to display when the current value is `null` * default: Simple NonIdealState view */ zeroStateView?: JSX.Element; /** * Override the mosaicId passed to `react-dnd` to control how drag and drop works with other components * Note: does not support updating after instantiation * default: Random UUID */ mosaicId?: string; /** * Make it possible to use different versions of Blueprint with `mosaic-blueprint-theme` * Note: does not support updating after instantiation * default: 'bp3' */ blueprintNamespace?: string; /** * Override the react-dnd provider to allow applications to inject an existing drag and drop context */ dragAndDropManager?: DragDropManager | undefined; } export interface MosaicControlledProps<T extends MosaicKey> extends MosaicBaseProps<T> { /** * The tree to render */ value: MosaicNode<T> | null; onChange: (newNode: MosaicNode<T> | null) => void; } export interface MosaicUncontrolledProps<T extends MosaicKey> extends MosaicBaseProps<T> { /** * The initial tree to render, can be modified by the user */ initialValue: MosaicNode<T> | null; } export type MosaicProps<T extends MosaicKey> = MosaicControlledProps<T> | MosaicUncontrolledProps<T>;

MosaicWindow

export interface MosaicWindowProps<T extends MosaicKey> { title: string; /** * Current path to this window, provided by `renderTile` */ path: MosaicBranch[]; className?: string; /** * Controls in the top right of the toolbar * default: [Replace, Split, Expand, Remove] if createNode is defined and [Expand, Remove] otherwise */ toolbarControls?: React.ReactNode; /** * Additional controls that will be hidden in a drawer beneath the toolbar. * default: [] */ additionalControls?: React.ReactNode; /** * Label for the button that expands the drawer */ additionalControlButtonText?: string; /** * A callback that triggers when a user toggles the additional controls */ onAdditionalControlsToggle?: (toggle: boolean) => void; /** * Disables the overlay that blocks interaction with the window when additional controls are open */ disableAdditionalControlsOverlay?: boolean; /** * Whether or not a user should be able to drag windows around */ draggable?: boolean; /** * Method called when a new node is required (such as the Split or Replace buttons) */ createNode?: CreateNode<T>; /** * Optional method to override the displayed preview when a user drags a window */ renderPreview?: (props: MosaicWindowProps<T>) => JSX.Element; /** * Optional method to override the displayed toolbar */ renderToolbar?: ((props: MosaicWindowProps<T>, draggable: boolean | undefined) => JSX.Element) | null; /** * Optional listener for when the user begins dragging the window */ onDragStart?: () => void; /** * Optional listener for when the user finishes dragging a window. */ onDragEnd?: (type: 'drop' | 'reset') => void; }

The default controls rendered by MosaicWindow can be accessed from defaultToolbarControls

Advanced API

The above API is good for most consumers, however Mosaic provides functionality on the Context of its children that make it easier to alter the view state. All leaves rendered by Mosaic will have the following available on React context. These are used extensively by MosaicWindow.

/** * Valid node types * @see React.Key */ export type MosaicKey = string | number; export type MosaicBranch = 'first' | 'second'; export type MosaicPath = MosaicBranch[]; /** * Context provided to everything within Mosaic */ export interface MosaicContext<T extends MosaicKey> { mosaicActions: MosaicRootActions<T>; mosaicId: string; } export interface MosaicRootActions<T extends MosaicKey> { /** * Increases the size of this node and bubbles up the tree * @param path Path to node to expand * @param percentage Every node in the path up to root will be expanded to this percentage */ expand: (path: MosaicPath, percentage?: number) => void; /** * Remove the node at `path` * @param path */ remove: (path: MosaicPath) => void; /** * Hide the node at `path` but keep it in the DOM. Used in Drag and Drop * @param path */ hide: (path: MosaicPath) => void; /** * Replace currentNode at `path` with `node` * @param path * @param node */ replaceWith: (path: MosaicPath, node: MosaicNode<T>) => void; /** * Atomically applies all updates to the current tree * @param updates * @param suppressOnRelease (default: false) */ updateTree: (updates: MosaicUpdate<T>[], suppressOnRelease?: boolean) => void; /** * Returns the root of this Mosaic instance */ getRoot: () => MosaicNode<T> | null; }

Children (and toolbar elements) within MosaicWindow are passed the following additional functions on context.

export interface MosaicWindowContext<T extends MosaicKey> extends MosaicContext<T> { mosaicWindowActions: MosaicWindowActions; } export interface MosaicWindowActions { /** * Fails if no `createNode()` is defined * Creates a new node and splits the current node. * The current node becomes the `first` and the new node the `second` of the result. * `direction` is chosen by querying the DOM and splitting along the longer axis */ split: () => Promise<void>; /** * Fails if no `createNode()` is defined * Convenience function to call `createNode()` and replace the current node with it. */ replaceWithNew: () => Promise<void>; /** * Sets the open state for the tray that holds additional controls. * Pass 'toggle' to invert the current state. */ setAdditionalControlsOpen: (open: boolean | 'toggle') => void; /** * Returns the path to this window */ getPath: () => MosaicPath; /** * Enables connecting a different drag source besides the react-mosaic toolbar */ connectDragSource: (connectedElements: React.ReactElement<any>) => React.ReactElement<any>; }

To access the functions simply use the MosaicContext or MosaicWindowContext context consumers.

Mutating the Tree

Utilities are provided for working with the MosaicNode tree in mosaicUtilities and mosaicUpdates

MosaicUpdate

MosaicUpdateSpec is an argument meant to be passed to immutability-helper to modify the state at a path. mosaicUpdates has examples.

Upgrade Considerations / Changelog

See Releases

License

Copyright 2019 Kevin Verdieck, originally developed at Palantir Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the

编辑推荐精选

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具使用教程AI营销产品酷表ChatExcelAI智能客服
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

热门AI工具生产力协作转型TraeAI IDE
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

数据安全AI助手热门AI工具AI辅助写作AI论文工具论文写作智能生成大纲
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

热门AI工具AI办公办公工具智能排版AI生成PPT博思AIPPT海量精品模板AI创作
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流��量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

下拉加载更多