Function registerWidget

Enables automatic setting up of a widget from an elements matching the given selector.

If settings.autoWidgets is true, nothing is done. Otherwise, when an element matching the selector is added to the DOM, newWidget will be called and it's expected to setup the widget.

IMPORTANT: The widget that is returned by newWidget will be automatically destroyed when the element that created them is removed from the DOM.

IMPORTANT: If a widget by that name is already registered, the current call does nothing, even if the remaining arguments differ.

  • Type Parameters

    • Config extends Record<string, unknown>

    Parameters

    • name: string

      The name of the widget. Should be in kebab-case.

    • newWidget: WidgetCreateFn<Config>

      Called for every element matching the widget selector.

    • OptionalconfigValidator: null | WidgetConfigValidator<Config>

      A validator object, or a function that returns such an object, for all options supported by the widget. If given, then the newWidget function will also be passed a configuration object constructed from the element's data attribute.

    • Optionaloptions: { selector?: string; supportsMultiple?: boolean }
      • Optionalselector?: string

        The selector to match elements for. If not given, then uses a default value of [data-lisn-<name>], .lisn-<name>

      • OptionalsupportsMultiple?: boolean

        If true, and if configValidator is given, then the value of the element's widget specific data attribute will be split on ; and each one parsed individually as a configuration. Then the newWidget function will be called once for each configuration.

    Returns Promise<void>