Skip to content

Disable downloadLink()/downloadButton() until their href is ready - #4041

Merged
cpsievert merged 2 commits into
mainfrom
download-disable-until-ready
May 6, 2024
Merged

cpsievert merged 2 commits into
mainfrom
download-disable-until-ready

Conversation

@cpsievert

@cpsievert cpsievert commented May 3, 2024 •

Copy link
Copy Markdown
Collaborator

Closes #2614
Closes #3606

For reference https://getbootstrap.com/docs/5.3/components/buttons/#disabled-state

I've also tested this with Bootstrap 3 and it appears to work there as well.

TODO

  • Do a PR for py-shiny with corresponding markup changes

@cpsievert
cpsievert force-pushed the download-disable-until-ready branch from ce2c93b to 75cbb5e Compare May 3, 2024 20:49
@cpsievert
cpsievert requested a review from gadenbuie May 3, 2024 20:50
@cpsievert
cpsievert marked this pull request as ready for review May 3, 2024 20:50

@gadenbuie gadenbuie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't run the code, but I have read it carefully. It looks like very elegant fix to the problem! I'm glad to see this one getting fixed :)

@cpsievert
cpsievert merged commit ecb591f into main May 6, 2024
@cpsievert
cpsievert deleted the download-disable-until-ready branch May 6, 2024 14:37
cpsievert added a commit to posit-dev/py-shiny that referenced this pull request May 6, 2024
riccardoporreca added a commit to riccardoporreca/shiny that referenced this pull request Aug 19, 2025
…ink`

* This can fix rstudio#4119, where there existing logic would always re-enable the download button/link when the `href` is ready (rstudio#4041).
elnelson575 pushed a commit that referenced this pull request Apr 3, 2026
…load button auto-enable (#4119)

Frameworks like shinyjs, py-shiny, and custom JS can add the
data-shiny-disable-auto-enable attribute to a download button/link
to prevent Shiny from automatically removing the disabled class
and aria-disabled attribute on render.

This provides a generic, framework-agnostic opt-out for the
auto-enable behavior introduced in PR #4041.
elnelson575 pushed a commit that referenced this pull request Apr 9, 2026
…load button auto-enable (#4119)

Frameworks like shinyjs, py-shiny, and custom JS can add
`data-shiny-disable-auto-enable` to a download button/link to prevent
Shiny from automatically removing the `disabled` class and
`aria-disabled` attribute on render. This provides a generic,
framework-agnostic opt-out for the auto-enable behavior introduced
in PR #4041.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
elnelson575 pushed a commit that referenced this pull request May 7, 2026
…load button auto-enable (#4119)

Frameworks like shinyjs, py-shiny, and custom JS can add
`data-shiny-disable-auto-enable` to a download button/link to prevent
Shiny from automatically removing the `disabled` class and
`aria-disabled` attribute on render. This provides a generic,
framework-agnostic opt-out for the auto-enable behavior introduced
in PR #4041.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
cpsievert added a commit that referenced this pull request May 11, 2026
* feat: add data-shiny-disable-auto-enable attribute to opt out of download button auto-enable (#4119)

Frameworks like shinyjs, py-shiny, and custom JS can add
`data-shiny-disable-auto-enable` to a download button/link to prevent
Shiny from automatically removing the `disabled` class and
`aria-disabled` attribute on render. This provides a generic,
framework-agnostic opt-out for the auto-enable behavior introduced
in PR #4041.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Revised approach

* chore: rebuild shiny.js bundle

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `devtools::document()` (GitHub Actions)

* `npm run build` (GitHub Actions)

* feat: rename autoUpdate to autoEnable, invert to data-ignore-update attr, block clicks on disabled download buttons

- Rename `autoUpdate` param to `autoEnable` in `downloadButton()`/`downloadLink()`
- Invert HTML attribute: `autoEnable=FALSE` now sets `data-ignore-update` (absent by default)
- Block click/auxclick events via preventDefault() when button has `disabled` class
- Update NEWS.md and add testthat tests for new attribute behavior

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `devtools::document()` (GitHub Actions)

* `npm run build` (GitHub Actions)

* chore: add test snapshots and rebuild source maps

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: block clicks on disabled download links via CSS and correct jQuery this binding

- Add `a.shiny-download-link.disabled { pointer-events: none }` to shiny.scss
  so disabled download links (not just btn-styled ones) block pointer events
- Fix click handler to use `this` instead of `e.currentTarget` — with jQuery
  delegated events, `e.currentTarget` is the document, not the matched element

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: update comment in downloadlink.ts

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `npm run build` (GitHub Actions)

* Update autoEnabled -> enabled

* tests: add unit and shinytest2 tests for downloadButton/Link enabled param

- Add missing `downloadLink starts disabled with correct attributes` unit test
- Add shinytest2 runtime tests covering all three `enabled` values for both
  downloadButton and downloadLink: initial state (auto-enable, stays disabled,
  starts enabled, shinyjs-disabled) and toggle on/off via a setEnabled custom
  message handler that mirrors shinyjs::enable()/disable()
- Clarify `enabled=FALSE` docs: the opt-out applies for the page lifetime via
  data-ignore-update, so renderValue never auto-enables regardless of runtime state
- Remove unreachable dead code in DownloadLinkOutputBinding.showProgress()

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Add to suggests

* `devtools::document()` (GitHub Actions)

* chore: fix review issues — add shinyjs to Suggests, correct docs, skip_on_cran

- Add shinyjs to DESCRIPTION Suggests (referenced in @PARAM and @Seealso)
- Add shinyjs::enable()/disable() to @Seealso
- Fix enabled="auto" doc: "non-NULL filename" was inaccurate; say "initialized the downloadHandler"
- Remove "(replacing autoEnable)" from NEWS — autoEnable was never public
- Add skip_on_cran() to shinytest2 browser test
- Fix trailing space in test app comment

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: remove shinyjs from seealso (already linked inline in param docs)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Update showProgress method signature in DownloadLinkOutputBinding

* Fix formatting of DownloadLinkOutputBinding class

* `npm run build` (GitHub Actions)

* fix: use e.currentTarget instead of this in download link click handler

Cast to HTMLAnchorElement for precise typing of .id and .href.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: revert showProgress and dead code cleanups per code review

Restore showProgress signature and return/e dead code to keep
the diff minimal per cpsievert's feedback.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: update generated type declaration for showProgress revert

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Update DESCRIPTION

Co-authored-by: Barret Schloerke <[email protected]>

* refactor: address code review feedback

- Use auto_enable/enabled logical vars in downloadButton/downloadLink
  instead of string comparisons; drop redundant else NULL
- Share single AppDriver across all shinytest2 tests (faster, ~12s vs ~33s)
- Add skip_on_os() to restrict shinytest2 tests to mac
- Remove redundant Config/Needs/check entry now that shinytest2 is in Suggests

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix AppDriver setup

* fix comment

* fix: revert formatting in downloadButton signature, add auxclick comment

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Replace jQuery event-driven output info with per-output ResizeObserver/IntersectionObserver (#3682)

* Use ResizeObserver/IntersectionObserver for per-output resize handling

Replace the old window-resize + Bootstrap event listener approach with
per-output ResizeObserver, IntersectionObserver, and MutationObserver.
Each bound output now gets its own observers that handle resize,
visibility, and theme changes independently, rather than relying on
global window resize events and jQuery Bootstrap hooks.

Also renames sendImageSize -> sendOutputInfo, simplifies bind.ts by
removing sendOutputHiddenState/maybeAddThemeObserver from BindInputsCtx,
and makes ImageOutputBinding.resize() actually set width/height on the
img element.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix observer cleanup, zoom-aware sizing, and edge cases in resize handling

- Disconnect ResizeObserver/IntersectionObserver/MutationObserver on unbind
  to prevent callbacks firing on stale elements
- Restore getBoundingClientSizeBeforeZoom for size reporting to fix CSS zoom
  regression (see #4135)
- Guard doTriggerResize against missing binding during teardown races
- Fix visibleOutputs set to properly remove hidden outputs
- Hoist observer setup out of doSendOutputInfo loop to avoid re-allocating
  closures on every call

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Fix pending output observer callbacks after unbind

* Flush pending output info before input send

* test and restore theme refresh for output observers

* fix: avoid theme mutation observers for non-theme outputs

* Replace custom isHidden() with native el.checkVisibility()

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Simplify observer setup: inline outputInfoObserver, consolidate cleanup, guard null IDs

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* Revert image resize width/height attr setting

The resize() method only needs to trigger the jQuery resize event for
brush re-projection. Setting width/height attrs on the <img> just
briefly stretched the stale plot before the server re-render replaced
it, with no meaningful effect on behavior.

* Restore image resize() to match main exactly

The previous commit over-scoped the revert by changing the method
signature. This restores the original signature from main.

* Remove review doc

* Skip doTriggerResize() during initial output info send

* Add isVisible() fallback for browsers without checkVisibility()

* `npm run build` (GitHub Actions)

* Update NEWS.md

* Remove unnecessary type cast in debounce cancel test

The debounce() function already returns DebouncedFunction with a
required `cancel` property. The cast to an optional `cancel` weakened
type checking.

* Defer sendOutputInfoFns.regular lookup to execution time

Wrap setTimeout callbacks with arrow functions so regular is resolved
at call time rather than captured when it may still be undefined.

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
Co-authored-by: cpsievert <[email protected]>

* `devtools::document()` (GitHub Actions)

* fix: address code review feedback

- Rename data-ignore-update to data-shiny-disable-auto-enable
- Separate auxclick handler from shiny:filedownload to preserve event semantics
- Move test-only deps (callr, devtools, httr, shinytest2) to Config/Needs/check

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: apply data-shiny-disable-auto-enable to enabled=TRUE as well

When `enabled = TRUE`, the attribute was previously omitted. Now it is
included for both `enabled = TRUE` and `enabled = FALSE`, since the
attribute's purpose is to signal any non-auto state — not just the
disabled state.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* refactor: replace sendCustomMessage with renderUI in test app, use input_switch

Replace the custom setEnabled JS message handler with renderUI()-based
toggling, which exercises the actual Shiny rendering/re-binding code
path including renderValue re-firing on element replacement. Replace
actionButton toggles with bslib::input_switch for clearer intent.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* docs: convert shinyjs doc links to plain text, remove from Suggests

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* style: use braces for auto_enable if/else block

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* style: convert enabled to logical in one line, drop if/else block

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: remove redundant auxclick handler

pointer-events:none CSS on disabled download links already blocks all
mouse interactions including middle-click, making the auxclick handler
unnecessary.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: simplify enabled param to default \"auto\", validate with cli_abort

Replace match.arg coercion with explicit is.logical() check and
cli::cli_abort() for a cleaner signature and better error message.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: move enabled param after icon in downloadButton signature

Per reviewer feedback (r3163231438).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: add shinytest2 browser tests for downloadButton/Link enabled param

- Add helper-shinytest2.R with shared skip_if_not_shinytest2() helper
- Add test-zzz-st2-download.R using AppDriver$new(generate_app) pattern
  with pkgload::load_all() to load the local dev package in subprocess
- Refactor test-downloadButton-shinytest2.R to use skip_if_not_shinytest2()

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: regenerate plotPNG.Rd

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: add shinyjs to Config/Needs/check

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: use shinyjs::disabled() in shinyjs test cases, explain subprocess loading

- Replace htmltools::tagAppendAttributes(class = "shinyjs-disabled") with
  shinyjs::disabled() in both test app and test-zzz-st2-download.R
- Add shinyjs::useShinyjs() to test app UIs
- Add comment explaining why pkgload::load_all() is called explicitly in
  generate_app (shinytest2 shims library() but those shims rely on shiny
  being available for shiny::runApp(); we load it directly to bootstrap)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: trim generate_app comment

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: fix generate_app comment (shim reason, not jQuery timing)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: remove redundant shinytest2 test and app

test-zzz-st2-download.R covers the same behavior via AppDriver$new(generate_app),
making the callr+URL approach in test-downloadButton-shinytest2.R unnecessary.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: remove callr and httr from check deps (no longer used in tests)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* test: update shinyjs test descriptions to reflect actual mechanism

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: switch shinytest2 test to callr+URL approach to avoid jQuery timing issue

AppDriver$new(fn) has the same timing issue as AppDriver$new(app_dir):
shinytest2's tracer injects before jQuery/Shiny are ready when the package
is devtools-loaded. We work around this by starting the app in a callr
subprocess, polling until ready, then connecting via URL.

Also restores callr and httr to Config/Needs/check and helper skip guard.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: use callr+URL approach for shinytest2, switch to checkboxInput toggles

AppDriver$new(fn) has a timing race in the devtools-loaded context: it
navigates immediately after the port opens, before the app has finished
serving all static resources. For complex apps this causes shinyjs's inline
handlers to fire before shiny.min.js loads, leaving Shiny undefined.

The callr+HTTP polling approach avoids this by waiting for a successful
GET before connecting AppDriver to the already-running app.

bslib::input_switch is replaced with checkboxInput for the toggle controls.
bslib adds a <script type="module"> (web-components.min.js) which is deferred
and also contributes to the resource-loading race in this context.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Fixing testing issue

* Update helper

* chore: reset index.ts files to main and rebuild JS

The rebase with -X theirs kept an intermediate version of the #3682
ResizeObserver commit that was already superseded in main. Reset
srcts/src/shiny/index.ts and srcts/src/utils/index.ts to main's
versions and rebuild the JS bundles.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `devtools::document()` (GitHub Actions)

* Update docs and tests

* chore: reset unrelated Rd files to main

A GitHub Actions devtools::document() commit in the branch regenerated
all Rd files, adding noise to the PR diff. Reset all Rd files not
related to this feature (downloadButton/downloadLink) back to main.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: clean up DESCRIPTION and NEWS noise from rebase

- Remove devtools from Config/Needs/check (not actually used in tests)
- Restore RoxygenNote: 7.3.3 and remove Config/roxygen2/version added
  by GitHub Actions devtools::document() commit
- Restore three NEWS bullet points dropped by -X theirs rebase

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: restore Roxygen/RoxygenNote field order in DESCRIPTION

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* refactor: use tagGetAttribute in tests, update NEWS wording

- Replace regex expect_match/expect_no_match with htmltools::tagGetAttribute()
  for more precise attribute assertions
- Update NEWS entry to cpsievert's suggested wording

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* fix: guard empty href clicks and skip if shinyjs not installed

- Prevent early clicks on enabled=TRUE buttons before renderValue sets
  the URL (href="" would otherwise navigate to the current page)
- Add skip_if_not_installed("shinyjs") to skip_if_not_shinytest2() so
  tests skip cleanly in envs where shinyjs is not available

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `devtools::document()` (GitHub Actions)

* chore: revert GitHub Actions devtools::document() noise in DESCRIPTION

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* chore: reset unrelated Rd files to main (GitHub Actions noise)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* refactor: move shinyjs skip guard to test file that uses it

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `devtools::document()` (GitHub Actions)

* chore: reset GitHub Actions devtools::document() noise

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* `devtools::document()` (GitHub Actions)

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

* Fix R CMD check warning for unstated test dependencies

Move shinytest2 to Suggests; access shinyjs indirectly via
getFromNamespace() to avoid the static pattern scanner.

* `usethis::use_tidy_description()` (GitHub Actions)

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Co-authored-by: elnelson575 <[email protected]>
Co-authored-by: Barret Schloerke <[email protected]>
Co-authored-by: Carson Sievert <[email protected]>
Co-authored-by: cpsievert <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: cpsievert <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants