Saltar al contenido principal
Version: 2.x

Componentes Interactivos

A menudo, querrá activar componentes interactivos además de cualquier contenido/animación que tenga en una página determinada. Dependiendo de la naturaleza de su extensión, puede querer definir elementos interactivos personalizados o reutilizar o extender los existentes.

All components and utilities from Flarum core and bundled extensions are exported, making them available for reuse in other extensions. A full list is available in our API documentation.

Alertas​

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

  • La aplicación app.alerts.show añadirá una nueva alerta, y devolverá una clave que puede ser utilizada posteriormente para descartar esa alerta. Tiene 3 sobrecargas:
    • app.alerts.show(children)
    • app.alerts.show(attrs, children)
    • app.alerts.show(componentClass, attrs, children)
  • app.alerts.dismiss(key) desechará una alerta activa con la clave dada, si es que existe.
  • app.alerts.clear() descartará todas las alertas.
  • app.alerts.getActiveAlerts() returns the alerts currently being shown.
  • app.alerts.showLoading() and app.alerts.clearLoading() bracket a slow operation, showing a shared loading alert while it runs. See below.

Loading Indicator​

showLoading() and clearLoading() are a pair: call the first when a slow operation starts and the second when it finishes, including when it fails.

They are reference counted, so concurrent operations share one indicator rather than stacking up several, and the indicator only appears after a short delay. An operation that finishes quickly never shows one at all, which avoids a flicker on every fast request.

app.alerts.showLoading();

try {
await doSomethingSlow();
} finally {
app.alerts.clearLoading();
}

Always clear in a finally, or an early return leaves the count raised and the indicator stuck on screen for the rest of the page's life.

Normalmente, no necesitará un componente personalizado para las alertas; sin embargo, si lo desea, puede proporcionar uno. Probablemente querrá que herede de flarum/components/Alert.

Los siguientes atrs son útiles para tener en cuenta:

  • El attr type aplicará la clase css Alert--{type}. Si la alerta es success, la alerta será verde, si es error, roja, y si está vacía type, amarilla.
  • El attr dismiss dictará si se mostrará un botón de desestimación.
  • El attr ondismiss se puede utilizar para proporcionar una llamada de retorno que se ejecutará cuando la alerta sea descartada.
  • Los componentes proporcionados en el attr controls se mostrarán después de los hijos de la alerta.

Modales​

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

  • app.modal.show(componentClass, attrs, stackModal) will show a modal using the given component class and attrs. By default, showing a modal while another is open replaces it. Pass true as the third argument to stack the new modal on top of the current one instead.
  • app.modal.close() will close the topmost open modal. With stacked modals, this reveals the one underneath rather than closing everything.
  • app.modal.isModalOpen() returns whether any modal is currently open.

show() is asynchronous, and accepts either a modal component class or a function returning a promise of one. The second form is how a modal is code split out of your main bundle, with the manager showing a loading state while the chunk arrives:

app.modal.show(() => import('./components/MyCoolModal'), { attr: 'value' });
Showing a modal from a lifecycle method

Calling app.modal.show() directly inside oncreate, view or another lifecycle method does not work reliably, because of how Mithril handles a redraw triggered during a redraw. Defer it by a tick:

setTimeout(() => app.modal.show(MyCoolModal, { attr: 'value' }), 0);

A diferencia de las alertas, la mayoría de los modales utilizarán una clase personalizada, heredando flarum/components/Modal. Por ejemplo:

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

export default class CustomModal extends Modal {
// All true by default. These control whether the modal can be dismissed via the close button, the Esc key, and clicking the backdrop, respectively.
static isDismissibleViaCloseButton = true;
static isDismissibleViaEscKey = true;
static isDismissibleViaBackdropClick = 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>;
}
}

Modals with forms inherit flarum/common/components/FormModal. This class provides a onsubmit method which is called when the submit button is clicked:

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

export default class CustomFormModal extends FormModal {
// All true by default. These control whether the modal can be dismissed via the close button, the Esc key, and clicking the backdrop, respectively.
static isDismissibleViaCloseButton = true;
static isDismissibleViaEscKey = true;
static isDismissibleViaBackdropClick = 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.

You can use the CLI to automatically generate a modal:

$ flarum-cli make frontend modal

Composer​

Como Flarum es un foro, necesitamos herramientas para que los usuarios puedan crear y editar mensajes y discusiones. Flarum logra esto a través del componente compositor flotante.

The composer is managed by a global instance of ComposerState, which is accessible via app.composer on the forum frontend. Sus métodos públicos más importantes son:

  • app.composer.load(componentClass, attrs) cargará un nuevo tipo de compositor. Si un compositor ya está activo, será reemplazado.
  • app.composer.show() mostrará el compositor si está actualmente oculto.
  • app.composer.close() cerrará y reiniciará el compositor después de confirmar con el usuario.
  • app.composer.hide() cerrará y reiniciará el compositor sin confirmar con el usuario.
  • app.composer.bodyMatches(componentClass, attrs) comprobará si el compositor actualmente activo es de un tipo determinado, y si sus atts coinciden con los attrs proporcionados opcionalmente.

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

Debido a que el compositor puede ser utilizado para varias acciones diferentes (iniciar una discusión, editar un mensaje, responder a una discusión, etc.), sus campos pueden variar dependiendo del uso. Esto se hace dividiendo el código para cada uso en una subclase de flarum/components/ComposerBody. Esta clase de componente debe ser proporcionada cuando se carga un compositor.

Editor del Composer​

The actual editor is yet another component, flarum/common/components/TextEditor. Its state can be programatically accessed via an "editor driver", which implements EditorDriverInterface. Está disponible globalmente para el compositor actual a través de app.composer.editor. Tiene una variedad de métodos públicos que permiten a las extensiones insertar y modificar mediante programación el contenido actual, las selecciones y la posición del cursor del editor de texto del compositor activo.