Skip to content

Commit b5ffab9

Browse files
authored
fix: Ensure paired server warning is removed (#47)
1 parent b29a084 commit b5ffab9

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to `shinyswatch` will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
### Bug fixes
11+
12+
* The theme picker's message warning users to include `shinyswatch.theme_picker_server()` is now correctly hidden if the app takes longer than expected to start up. (#47)
13+
814
## [0.7.0] - 2024-07-18
915

1016
### Breaking changes

shinyswatch/_theme_picker.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,23 @@ def theme_picker_ui(default: DEPRECATED_PARAM = DEPRECATED) -> ui.TagChild:
6363
"Please use the `theme` argument of a Shiny page function to set the initial theme.",
6464
)
6565

66-
return ui.tags.div(
66+
return ui.TagList(
6767
# Have a div that is hidden by default and is shown if the server does not
6868
# disable it. This is nice as the warning will be displayed if the server method
6969
# is not run.
7070
ui.div(
71-
"!! Please include `shinyswatch.theme_picker_server()` in your server function !!",
72-
style="color: var(--bs-danger); background-color: var(--bs-light); display: none;",
71+
ui.div(
72+
ui.HTML("&#9888;<br>"), # warning triangle
73+
"Please include ",
74+
ui.code(
75+
"shinyswatch.theme_picker_server()",
76+
style="overflow-wrap: anywhere;",
77+
),
78+
" in your server function.",
79+
),
80+
style="display: none;",
81+
class_="alert alert-danger align-items-center",
82+
role="alert",
7383
id="shinyswatch_picker_warning",
7484
),
7585
ui.input_select(
@@ -124,9 +134,14 @@ def server(input):
124134
session = require_active_session(None)
125135
input = session.input
126136

137+
async def remove_theme_picker_warning():
138+
await session.send_custom_message("shinyswatch-hide-warning", {})
139+
127140
@reactive.effect
128141
@reactive.event(input.__shinyswatch_initial_theme)
129-
def _():
142+
async def _():
143+
await remove_theme_picker_warning()
144+
130145
init_theme_name = input.__shinyswatch_initial_theme()["name"]
131146
last_theme = input.__shinyswatch_initial_theme()["saved"]
132147

@@ -171,4 +186,4 @@ async def _():
171186

172187
@reactive.effect
173188
async def _():
174-
await session.send_custom_message("shinyswatch-hide-warning", {})
189+
await remove_theme_picker_warning()

shinyswatch/picker/theme_picker.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* globals Shiny */
1+
/* globals Shiny,$ */
22
(function () {
33
// Stores the default theme links, if not one of the shinyswatch themes
44
// Is always an array, even if there is only one link
@@ -211,13 +211,28 @@
211211
window.Shiny.setInputValue('__shinyswatch_initial_theme', initTheme)
212212
}
213213

214-
const displayWarning = setTimeout(function () {
215-
window.document.querySelector('#shinyswatch_picker_warning').style.display =
216-
'block'
217-
}, 1000)
214+
function removeWarning() {
215+
const warning = document.getElementById("shinyswatch_picker_warning")
216+
if (warning) {
217+
warning.remove()
218+
}
219+
}
220+
221+
function showWarning() {
222+
const warning = document.getElementById('shinyswatch_picker_warning')
223+
if (warning) {
224+
warning.style.display = null
225+
}
226+
}
227+
228+
if (typeof window.Shiny.setInputValue === "function") {
229+
setTimeout(showWarning, 1000)
230+
} else {
231+
$(window).one("shiny:idle", showWarning)
232+
}
218233

219234
Shiny.addCustomMessageHandler('shinyswatch-hide-warning', function (_) {
220-
window.clearTimeout(displayWarning)
235+
removeWarning()
221236
})
222237

223238
Shiny.addCustomMessageHandler('shinyswatch-pick-theme', replaceShinyswatchCSS)

0 commit comments

Comments
 (0)