返回介绍

Interactive Components

发布于 2024-06-22 20:04:58 字数 7204 浏览 0 评论 0 收藏 0

Often, you'll want to trigger interactive components in addition to whatever content/animations you have on a given page. Depending on the nature of your extension, you may want to define custom interactive elements or reuse or extend existing ones.

Remember that all components used in Flarum core are exported and made available for extensions to reuse. A full list is available in our API documentation.

Alerts

Alerts are managed by a global instance of AlertManagerState, which is accessible via app.alerts on both the forum and admin frontends. It has 3 publicly accessible methods:

  • app.alerts.show will add a new alert, and return a key which can later be used to dismiss that alert. It has 3 overloads:
    • app.alerts.show(children)
    • app.alerts.show(attrs, children)
    • app.alerts.show(componentClass, attrs, children)
  • app.alerts.dismiss(key) will dismiss an active alert with the given key, if one exists.
  • app.alerts.clear() will dismiss all alerts.

Typically, you won't need a custom component for alerts; however, if you could like, you can provide one. You'll probably want it to inherit flarum/common/components/Alert.

The following attrs are useful to keep in mind:

  • The type attr will apply the Alert--{type} css class. success will yield a green alert, error a red alert, and an empty type a yellow alert.
  • The dismissible attr will dictate whether a dismiss button will be shown.
  • The ondismiss attr can be used to provide a callback which will run when the alert is dismissed.
  • Components provided in the controls attr will be shown after alert children.

Modals

Modals are managed by a global instance of ModalManagerState, which is accessible via app.modal on both the forum and admin frontends. It has 2 publicly accessible methods:

  • app.modal.show(componentClass, attrs) will show a modal using the given component class and attrs. If called while a modal is already open, it will replace the currently open modal.
  • app.modal.close() will close the modal if one is currently active.

As opposed to alerts, most modals will use a custom class, inheriting flarum/common/components/Modal. For example:

import Modal from 'flarum/common/components/Modal';

export default class CustomModal extends Modal {
  // True by default, dictates whether the modal can be dismissed by clicking on the background or in the top right corner.
  static isDismissible = true;

  className() {
    // Custom CSS classes to apply to the modal
    return 'custom-modal-class';
  }

  title() {
    // Content to show in the modal's title bar
    return <p>Custom Modal</p>;
  }

  content() {
    // Content to show in the modal's body
    return <p>Hello World!</p>;
  }

  onsubmit() {
    // If your modal contains a form, you can add form processing logic here.
  }
}

More information about methods available to override is available in our API documentation.

Flarum CLI

You can use the CLI to automatically generate a modal:

$ flarum-cli make frontend modal

Composer

Since Flarum is a forum, we need tools for users to be able to create and edit posts and discussions. Flarum accomplishes this through the floating composer component.

The composer is managed by a global instance of ComposerState, which is accessible via app.composer on the forum frontend. Its most important public methods are:

  • app.composer.load(componentClass, attrs) will load in a new composer type. If a composer is already active, it will be replaced.
  • app.composer.show() will show the composer if it is currently hidden.
  • app.composer.close() will close and reset the composer after confirming with the user.
  • app.composer.hide() will close and reset the composer without confirming with the user.
  • app.composer.bodyMatches(componentClass, attrs) will check if the currently active composer is of a certain type, and whether its atts match optionally provided attrs.

The full list of public methods is documented in the API docs linked above.

Because the composer can be used for various different actions (starting a discussion, editing a post, replying to a discussion, etc.), its fields may vary depending as usage. This is done by splitting code for each usage into a subclass of flarum/forum/components/ComposerBody. This component class must be provided when loading a composer.

Composer Editor

The actual editor is yet another component, flarum/common/components/TextEditor. Its state can be programatically accessed via an "editor driver", which implements EditorDriverInterface. This is globally available for the current composer via app.composer.editor, and allows extensions to programatically read, insert, and modify the current contents, selections, and cursor position of the active composer's text editor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文