@flourish/controls

中文文档
11.0.1发布于 2026/09/01LicenseRef-LICENSEESM + CJS近 30 天下载 5889

Switchable dropdown/buttons/slider control

原始使用文档
文档目录22 节
  1. How to install
  2. Basics
  3. Controls container methods
  4. appendTo(parent_container)
  5. add([controls_instances])
  6. update()
  7. Controls methods
  8. appendTo(parent_container)
  9. format(formatterFunction, exclude)
  10. getNode()
  11. getSortedIndex()
  12. index([i])
  13. n_options()
  14. on(event, callback)
  15. options([arr])
  16. parse(parserFunction)
  17. remove()
  18. trigger(event)
  19. update()
  20. value([val])
  21. visible([boolean])
  22. Styling the controls

Flourish controls

Add interchangeable controls (dropdown, buttons or slider) and controls container to a page.

How to install

npm install -s @flourish/controls

Add one or more control objects to your template state as well an object for the controls container

export var state = {
    controls_container: {},
    buttons_control: {
        control_type: "buttons"
    },
    dropdown_control: {
        control_type: "dropdown"
    },
    ...
}

Add settings for the controls container in template.yml.

  - property: controls_container
    import: "@flourish/controls/container"

This will add a controls container alignment and spacing options in the settings.

Then import @flourish/controls for each controls instance.

- property: buttons_control
    import: "@flourish/controls"
- property: dropdown_control
    import: "@flourish/controls"

Basics

Initialise the controls container and individual controls outside any function:

import { createControlsContainer, createControls } from "@flourish/controls";

var buttons_control = createControls(state.buttons_control);
var dropdown_control = createControls(state.dropdown_control)

var controls_container = createControlsContainer(state.controls_container)
    .appendTo(layout.getSection("controls"))
    .add([buttons_control, dropdown_control]);

You can add an id string to a control using the second argument:

var buttons_control = createControls(state.buttons_control, "fl-buttons-control");

Add an on change handler to each controls instance: buttons_control.on("change", update);. This is usually done in the draw function.

In update you typically want to update the set of options and then call update method on the container and control(s):

controls_container.update();

buttons_control
    .options(button_options)
    .update();

dropdown_control
    .options(dropdown_options)
    .update();

If you're using this module alongside @flourish/ui-styles you should call the update method from the ui-styles object first (this will ensure the dropdown icon correctly matches the size and color styles set in the ui-styles panel). For example:

function updateControlStyles() {
    controls_style.update();
    button_style.update();
    dropdown_style.update();
    slider_style.update();
}

updateControlStyles();

Controls container methods

appendTo(parent_container)

Appends the controls container to the parent_container DOM node (this should usually be layout.getSection("controls")). Returns the controls container object.

add([controls_instances])

Takes an array of controls instances and appends these to the controls container. Returns the controls container object.

update()

This updates the controls container and all the appended controls instances (calling their individual update() methods). Returns the controls container object.

Controls methods

appendTo(parent_container)

Appends the control to the parent_container DOM node and injects CSS into the head if necessary.

Returns the control object.

format(formatterFunction, exclude)

Specify an output formatter to format the contents of the control. This takes a function from the @flourish/formatters module or an output_format_id from the @flourish/interpreter module. Optionally pass an array of values to exclude from formatting.

getNode()

Returns the container of the control that has been created. It will have a class of "fl-controls-container"

getSortedIndex()

Returns the index of the currently chosen value in a sorted version of the options array.

index([i])

With no argument returns the (unsorted) index of the currently selected option. If i is specified and corresponds to an index into the options array, sets the currently selected option to i. Returns the control object.

n_options()

Returns the current number of options.

on(event, callback)

Add callback to the list of event handlers. Currently the only supported event is "change". Returns the control object.

options([arr])

With no argument returns a copy of the current list of options displayed by the current control. With an array, replaces the current options with a copy of arr and returns the control object.

parse(parserFunction)

Specify a function to parse the contents of the control. This takes a function from the @flourish/localization or @flourish/interpreter module.

remove()

Removes the control (and its resize event handler) from the DOM. Returns the control object.

trigger(event)

Calls all the handlers assigned to event and returns the control object. Currently the only supported event type is "change".

update()

Rebuilds the control with latest settings. Returns the control object.

value([val])

With no argument returns the string value of the currently selected option. With val present changes the current index to match that of val in the options array and returns the control object. If val is not in the options array then nothing is changed but the control object is still returned.

visible([boolean])

Set the visibility of the control

Styling the controls

The controls have very basic styling. You can overwrite these styles with your own css and styling. We recommend using @flourish/ui-styles to style the controls.

版本与发布记录

@flourish/controls 共发布 61 个版本,首次发布于 2017/09/27;过去 12 个月发布了 6 个版本。

版本发布日期距今
11.0.1latest2026/09/012 个月前
11.0.02026/06/115 个月前
10.0.12026/06/115 个月前
10.0.02026/06/115 个月前
9.1.12026/05/285 个月前
9.1.02026/03/267 个月前

其他发布通道:next → 2.0.0-prerelease.1。

关于 @flourish/controls 的常见问题

@flourish/controls 是什么?

Switchable dropdown/buttons/slider control

如何安装 @flourish/controls?

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

@flourish/controls 支持 TypeScript 吗?

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

@flourish/controls 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flourish/controls 可以商用吗?

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

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

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

@flourish/controls 还在维护吗?

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