Skip to content

Commit 098ffb8

Browse files
committed
Add USE_SHADOW_DOM setting to opt-out to shadow DOM toolbar rendering
1 parent d75cd33 commit 098ffb8

6 files changed

Lines changed: 17 additions & 9 deletions

File tree

debug_toolbar/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def _is_running_tests():
3232
"ROOT_TAG_EXTRA_ATTRS": "",
3333
"SHOW_COLLAPSED": False,
3434
"SHOW_TOOLBAR_CALLBACK": "debug_toolbar.middleware.show_toolbar",
35+
"USE_SHADOW_DOM": True,
3536
"TOOLBAR_LANGUAGE": None,
3637
"TOOLBAR_STORE_CLASS": "debug_toolbar.store.MemoryStore",
3738
"UPDATE_ON_FETCH": False,

debug_toolbar/static/debug_toolbar/css/toolbar.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Variable definitions */
2-
:host {
2+
#djDebug {
33
/* Font families are the same as in Django admin/css/base.css */
44
--djdt-font-family-primary:
55
"Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif,
@@ -13,7 +13,7 @@
1313
"Noto Color Emoji";
1414
}
1515

16-
:host,
16+
#djDebug,
1717
#djDebug[data-theme="light"] {
1818
--djdt-font-color: black;
1919
--djdt-background-color: white;

debug_toolbar/static/debug_toolbar/js/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ function getDebugElement() {
7676
// everywhere the element is being selected. A fixed reference
7777
// to the element should be avoided because the entire DOM could
7878
// be reloaded such as via HTMX boosting.
79-
const root = document.getElementById(
80-
"djDebugShadowRootContainer"
81-
).shadowRoot;
79+
let root = document.getElementById("djDebugRoot");
80+
if (root.shadowRoot) {
81+
root = root.shadowRoot;
82+
}
8283
return root.querySelector("#djDebug");
8384
}
8485

debug_toolbar/templates/debug_toolbar/base.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% load i18n static %}
2-
<div id="djDebugShadowRootContainer" {{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
3-
<template shadowrootmode="open">
2+
<div id="djDebugRoot" {{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
3+
{% if use_shadow_dom %}<template shadowrootmode="open">{% endif %}
44
{% block css %}
55
<link{% if toolbar.csp_nonce %} nonce="{{ toolbar.csp_nonce }}"{% endif %} rel="stylesheet" href="{% static 'debug_toolbar/css/print.css' %}" media="print">
66
<link{% if toolbar.csp_nonce %} nonce="{{ toolbar.csp_nonce }}"{% endif %} rel="stylesheet" href="{% static 'debug_toolbar/css/toolbar.css' %}">
@@ -42,5 +42,5 @@
4242
{% endfor %}
4343
<div id="djDebugWindow" class="djdt-panelContent djdt-hidden"></div>
4444
</div>
45-
</template>
45+
{% if use_shadow_dom %}</template>{% endif %}
4646
</div>

debug_toolbar/toolbar.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ def render_toolbar(self) -> str:
9797
if not self.should_render_panels():
9898
self.init_store()
9999
try:
100-
context = {"toolbar": self}
100+
context = {
101+
"toolbar": self,
102+
"use_shadow_dom": self.config["USE_SHADOW_DOM"],
103+
}
101104
lang = self.config["TOOLBAR_LANGUAGE"] or get_language()
102105
with lang_override(lang):
103106
return render_to_string("debug_toolbar/base.html", context)

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Pending
88
fetch requests.
99
* Added a note to the prerequisites section of the installation docs
1010
about requiring an up-to-date browser.
11+
* Updated to render the toolbar in a shadow DOM for better isolation
12+
from the rest of the page. This can be disabled with the setting
13+
``USE_SHADOW_DOM``.
1114

1215
6.3.0 (2026-04-01)
1316
------------------

0 commit comments

Comments
 (0)