Skip to content

Interface for createContentScriptUi does not align with documentation #267

Closed

Description

Describe the bug

Very impressed with this framework btw, nice work.

This regards the method createContentScriptUi defined in https://wxt.dev/entrypoints/content-scripts.html#ui.

The docs refer to two methods mount and onRemove. There are a few discrepancies in the TypeScript interface:

  • mount should take a parameter container: Element
  • The interface defines a method remove, but not onRemove
  • The docs say that remove/onRemove take a parameter onRemove(app), but this isn't in the interface and I'm not sure what this should be

export interface ContentScriptUi<TApp> {
/**
* The `HTMLElement` hosting the shadow root used to isolate the UI's styles. This is the element
* that get's added to the DOM. This element's style is not isolated from the webpage.
*/
shadowHost: HTMLElement;
/**
* The container element inside the `ShadowRoot` whose styles are isolated. The UI is mounted
* inside this `HTMLElement`.
*/
uiContainer: HTMLElement;
/**
* The shadow root performing the isolation.
*/
shadow: ShadowRoot;
/**
* Custom data returned from the `options.mount` function.
*/
mounted: TApp;
/**
* Function that mounts or remounts the UI on the page.
*/
mount: () => void;
/**
* Function that removes the UI from the webpage.
*/
remove: () => void;
}

Expected behavior

The interface aligns with the docs and these parameters are defined

Activity

  1. ghost added
    pending-triage
    on Dec 9, 2023
  2. aklinker1 commented on Dec 9, 2023

    @aklinker1
    Member

    Thanks, glad you like it! 😊

    The docs for createContentScriptUi and createContentScripIframe could be cleaned up and improved... They are confusing, and a full example would help.

    But it does behave as described.

    I think the thing that's confusing you is that there's two mount functions. One gets passed into createContentScriptUi, the other is called from the return value.

    The interface you shared is the type for the return type, which only includes mount and remove.

    Right underneath it is the options interface that gets passed into the function.

    export type ContentScriptUiOptions<TApp> = ContentScriptPositioningOptions &
    ContentScriptAnchoredOptions & {
    /**
    * The name of the custom component used to host the ShadowRoot. Must be kebab-case.
    */
    name: string;
    /**
    * Callback executed when mounting the UI. This function should create and append the UI to the
    * `container` element. It is called every time `ui.mount()` is called
    *
    * Optionally return a value that can be accessed at `ui.mounted` or in the `onRemove` callback.
    */
    mount: (container: Element) => TApp;
    /**
    * Callback called when the UI is removed from the webpage. Use to cleanup your UI, like
    * unmounting your vue or react apps.
    */
    onRemove?: (mounted: TApp) => void;
    /**
    * Custom CSS text to apply to the UI. If your content script imports/generates CSS and you've
    * set `cssInjectionMode: "ui"`, the imported CSS will be included automatically. You do not need
    * to pass those styles in here. This is for any additional styles not in the imported CSS.
    *
    * See https://wxt.dev/entrypoints/content-scripts.html#ui for more info.
    */
    css?: string;
    };

    This is the interface with mount(container): app and onRemove(app).

    • mount should take a parameter container: Element

    This mount function is passed into createContentScriptUi, and cannot be called from the return value. It is used to mount your UI inside the ShadowRoot.

    Anchor and alignment are used to position your UI on the page. That is why the mount function on the return type provides no arguments - you've already defined where the UI will be mounted.

    • The interface defines a method remove, but not onRemove

    onRemove is passed into createContentScriptUi, not called on the return value.

    • The docs say that remove/onRemove take a parameter onRemove(app), but this isn't in the interface and I'm not sure what this should be

    The remove function on the return value doesn't take any parameters because, once again, you already defined the onRemove behavior when calling the creayeContentScriptUi.


    I will improve the docs and add an example. I'd really appreciate your feedback once complete, it's hard to judge how understandable docs you wrote are 😅

  3. added
    docs
    and removed
    pending-triage
    on Dec 9, 2023
  4. ghost closed this as completedon Dec 10, 2023
  5. aklinker1 commented on Dec 10, 2023

    @aklinker1
    Member

    @AndrewWalsh I published the new docs. When you have time, let me know what you think! Thanks

    https://wxt.dev/guide/content-script-ui.html

  6. aklinker1 commented on Dec 10, 2023

    @aklinker1
    Member

    Great feedback! I'll update the docs today.

  7. aklinker1 commented on Dec 12, 2023

    @aklinker1
    Member

    I've added your suggestions in #272.

    I want to add a helper function for integrated UIs, similar to createContentScriptUi and createContentScriptIframe, but I'm recognizing that adding a third function would be confusing and that the API reference is hard to follow because of how I've constructed the options type.

    So I think I'm going to simplify all that into a single function with a basic type for the options. Something like this:

    export function createContentScriptUi(
      options: {
        mode?: "integrated" | "shadow-root" | "iframe",
        position?: "inline" | "overlay" | "modal",
        zIndex?: number;
        anchor?: string | Element;
        alignment?: "top-left" | "top-right" | "bottom-right" | "bottom-left";
        page?: string;
        css?: string;
        name?: string;
        mount?(container: Element): TApp;
        onRemove?(app: TApp): void;
      },
    ): ContentScriptUi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions