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.
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.
yarn add react-mosaic-component
react-mosaic-component.css
is included on your page.Mosaic
component and use it in your app.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.
Mosaic supports the Blueprint Dark Theme out of the box when rendered with the mosaic-blueprint-theme bp3-dark
class.
html, body, #app { height: 100%; width: 100%; margin: 0; }
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).
T
s 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.
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.
Mosaic views have two modes, similar to React.DOM
input elements:
All of the previous examples show use of Mosaic in an Uncontrolled fashion.
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.
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
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.
Utilities are provided for working with the MosaicNode tree in mosaicUtilities
and
mosaicUpdates
MosaicUpdateSpec
is an argument meant to be passed to immutability-helper
to modify the state at a path.
mosaicUpdates
has examples.
See Releases
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
AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更 快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻 松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。
专业的AI公文写作平台,公文写作神器
AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。
最新AI工具、AI资讯
独家AI资源、AI项目落地
微信扫一扫关注公众号