@flourish/layout

中文文档
8.8.3发布于 2026/09/01LicenseRef-LICENSEES Module近 30 天下载 6275

Flourish module to control layouts

原始使用文档
文档目录20 节
  1. How to install
  2. How to use
  3. layout.getDefaultPrimaryHeight()
  4. layout.getOuterHeight([el])
  5. layout.getOuterWidth([el])
  6. layout.getOverlay()
  7. layout.getInnerHeight([el])
  8. layout.getInnerWidth([el])
  9. layout.getPrimaryHeight()
  10. layout.getPrimaryWidth()
  11. layout.getSection(el)
  12. layout.getSidebar()
  13. layout.getWrapper()
  14. layout.remToPx(rem)
  15. layout.setHeight(primary_height)
  16. layout.showOverlay(show_overlay)
  17. layout.update()
  18. layout.awaitFonts(callback, duration, font_weights)
  19. layout.prepareScreenshotSVG()
  20. layout.clearScreenshotSVG()

Flourish layout

Adds a wrapper and sections to a Flourish template with a host of settings and functions.

How to install

npm install -s @flourish/layout

Add a layout object to your template state:

export var state = {
    layout: {},
    ...
}

Import @flourish/layout into your template.yml settings.

How to use

Initialise the layout (only once) outside any function with a layout settings object:

import initLayout from "@flourish/layout";
var layout = initLayout(state.layout);

This adds a main wrapper to the page with 5 named sections: header, controls, legend, primary and footer. The primary section is a a flexible container that adjusts in height to fill the available space after the contents of the other sections has been taken into account. The initLayout call also adds a hidden overlay div to the primary that can be used to display a message if, for example, a template won't work in an old browser.

The layout object has a number of methods:

layout.getDefaultPrimaryHeight()

Returns the height the primary would be if using the standard Flourish breakpoints (unless in fixed_height mode, in which case it returns the actual available height). This can be useful in templates that switch between using standard Flourish breakpoints and using the setHeight method (see below).

layout.getOuterHeight([el])

Returns the height of the named section, including padding. If el is missing, the function returns the height of the wrapper node, including padding.

layout.getOuterWidth([el])

Returns the width of the named section, including padding. If el is missing, the function returns the width of the wrapper node, including padding.

layout.getOverlay()

Returns the (hidden-by-default) overlay div node.

layout.getInnerHeight([el])

Returns the height of the named section, excluding padding. If el is missing, the function returns the height of the wrapper node, excluding padding.

layout.getInnerWidth([el])

Returns the width of the named section, excluding padding. If el is missing, the function returns the width of the wrapper node, excluding padding.

layout.getPrimaryHeight()

Returns the height available for content in the primary container.

layout.getPrimaryWidth()

Returns the width available for content in the primary container.

layout.getSection(el)

Returns the container node inside section el. This is useful, for example, for adding a legend block to the legend section in the Flourish draw method. (Adding directly to the section should be avoided as this can make a mess of padding.)

layout.getSidebar()

Returns the sidebar node.

layout.getWrapper()

Returns the main wrapper node.

layout.remToPx(rem)

Returns the pixels for a rem value. This is useful when you need to add rem values in SVG for example.

layout.setHeight(primary_height)

If primary_height is a number then calculates the total_height required to fit the primary at that height and all the other sections at their current height into the page without scrollbars and calls Flourish.setHeight(total_height). If primary_height equals null then calls Flourish.setHeight(null).

layout.showOverlay(show_overlay)

If show_overlay is truthy this will show the overlay container, if it is falsy it will hide it. If show_overlay is a (non-empty) string then it will be used as the content of the paragraph element contained within the overlay. If show_overlay is a truthy value that is not a string then default text advising the user to upgrade their browser will be displayed.

layout.update()

Updates fonts, section order, titles and the footer based on the current values of the layout settings object.

layout.awaitFonts(callback, duration, font_weights)

Starts loading fonts and executes a function callback when all fonts have loaded (using the document.fonts.ready event listener). If an optional array of font_weights is provided invisible sample text will be appended for each font weight and then removed when a loading event which incudes that weight occurs. This stops the callback being triggered unnessecarily for each weight of a font. For example, a popup which includes a bold version of a font would trigger a font loading event if a template only contained fonts with a normal weight otherwise. font_weights defaults to ["normal", "bold"] (numeric values are also accepted). If all fonts are not loaded by the given duration (ms, which defaults to 500) then the callback is executed early. The callback is executed again when the fonts finally finish loading. Once the document.fonts.ready event has been triggered, a new event listener is setup to listen for further font loading events i.e. a new font is selected in the editor. This was introduced in v5.4. To update templates using previous versions of the module (with @flourish/font-watcher) to v5.4 and above follow the v5.4 migration guide.

layout.prepareScreenshotSVG()

Creates a temporary SVG containing header and footer text that will be included in the SVG export. Make sure to call this function in the template's screenshot function.

layout.clearScreenshotSVG()

Removes the temporary SVG created by layout.prepareScreenshotSVG()

版本与发布记录

@flourish/layout 共发布 88 个版本,首次发布于 2018/08/10;过去 12 个月发布了 4 个版本。

版本发布日期距今
8.8.3latest2026/09/012 个月前
8.8.22026/05/285 个月前
8.8.12026/05/225 个月前
8.8.02026/03/267 个月前
8.7.32025/11/121 年前
8.7.22025/10/231.1 年前

其他发布通道:next → 4.0.0-prerelease.4。

关于 @flourish/layout 的常见问题

@flourish/layout 是什么?

Flourish module to control layouts

如何安装 @flourish/layout?

在项目目录运行 npm install @flourish/layout,或使用 pnpm add / yarn add / bun add。

@flourish/layout 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/flourish__layout 是否存在。

@flourish/layout 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@flourish/layout 可以商用吗?

@flourish/layout 使用 LicenseRef-LICENSE 许可。声明的许可为 LicenseRef-LICENSE,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@flourish/layout 的下载量有多少?

根据 npm 官方下载统计,@flourish/layout 在 2026/10/15 – 2026/11/13 这 30 天内被下载 6,275 次,日均约 209 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flourish/layout 还在维护吗?

最新版本 8.8.3 发布于 2026/09/01(2 个月前);至今共发布 88 个版本,其中过去 12 个月发布了 4 个;首次发布于 2018/08/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。