Skip to content

Commit

Permalink
Avoid re-registering bslib components and globals (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed Apr 23, 2024
1 parent 45e063d commit 5046bbd
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 26 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# bslib (development version)

* Adjusted the border color of checkbox and radio buttons to match the border color of the input group in `bs_theme(preset="shiny")` . (#1038)
* Adjusted the border color of checkbox and radio buttons to match the border color of the input group in `bs_theme(preset="shiny")`. (#1038)

* `toggle_sidebar()` once again correctly closes a sidebar. (@fredericva, #1043)

* bslib now avoids re-defining its components when used in a context where they are already available, e.g. in a Quarto dashboard. (#1045)

# bslib 0.7.0

This large release includes many improvements and bug fixes for newer UI components like `layout_columns()`, `card()`, and `sidebar()`. In addition, the new `input_task_button()` offers a drop-in replacement for `shiny::actionButton()` (to prevent multiple submissions of the same operation) as well as pairing nicely with the new `shiny::ExtendedTask` for implementing truly non-blocking operations in Shiny.
Expand Down
16 changes: 12 additions & 4 deletions inst/components/dist/components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions inst/components/dist/components.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions inst/components/dist/components.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions inst/components/dist/components.min.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion inst/components/dist/web-components.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions inst/components/dist/web-components.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/components/dist/web-components.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions inst/components/dist/web-components.min.js.map

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions srcts/src/components/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ function registerBinding(
}
}

function registerBslibGlobal(name: string, value: object): void {
(window as any).bslib = (window as any).bslib || {};
if (!(window as any).bslib[name]) {
(window as any).bslib[name] = value;
} else {
console.error(
`[bslib] Global window.bslib.${name} was already defined, using previous definition.`
);
}
}

// Return true if the key exists on the object and the value is not undefined.
//
// This method is mainly used in input bindings' `receiveMessage` method.
Expand Down Expand Up @@ -91,6 +102,7 @@ async function shinyRenderContent(
export {
InputBinding,
registerBinding,
registerBslibGlobal,
hasDefinedProperty,
doWindowResizeOnElementResize,
getAllFocusableChildren,
Expand Down
5 changes: 2 additions & 3 deletions srcts/src/components/card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAllFocusableChildren, Shiny } from "./_utils";
import { getAllFocusableChildren, registerBslibGlobal, Shiny } from "./_utils";
import { ShinyResizeObserver } from "./_shinyResizeObserver";
import { ShinyRemovedObserver } from "./_shinyRemovedObserver";

Expand Down Expand Up @@ -449,7 +449,6 @@ class Card {
}

// attach Sidebar class to window for global usage
(window as any).bslib = (window as any).bslib || {};
(window as any).bslib.Card = Card;
registerBslibGlobal("Card", Card);

export { Card };
6 changes: 2 additions & 4 deletions srcts/src/components/sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputBinding, registerBinding } from "./_utils";
import { InputBinding, registerBinding, registerBslibGlobal } from "./_utils";
import { ShinyResizeObserver } from "./_shinyResizeObserver";

/**
Expand Down Expand Up @@ -528,7 +528,5 @@ class SidebarInputBinding extends InputBinding {
}

registerBinding(SidebarInputBinding, "sidebar");

// attach Sidebar class to window for global usage
(window as any).bslib = (window as any).bslib || {};
(window as any).bslib.Sidebar = Sidebar;
registerBslibGlobal("Sidebar", Sidebar);
8 changes: 7 additions & 1 deletion srcts/src/components/webcomponents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import { BslibSwitch, BslibSwitchInline } from "./switch";
BslibSwitch,
BslibSwitchInline,
].forEach((cls) => {
customElements.define(cls.tagName, cls);
if (!customElements.get(cls.tagName)) {
customElements.define(cls.tagName, cls);
} else {
console.error(
`[bslib] Custom element ${cls.tagName} was already defined, using previous definition.`
);
}
if (window.Shiny) {
if (cls.isShinyInput) makeInputBinding(cls.tagName);
if ("shinyCustomMessageHandlers" in cls) {
Expand Down

0 comments on commit 5046bbd

Please sign in to comment.