prevent JS error if [data-off-canvas-main-canvas] doesn't exist

Created on 5 March 2024, 4 months ago
Updated 17 June 2024, 7 days ago

Problem/Motivation

When the off_canvas_page_wrapper is disabled, ajax.js throws an error, caused by off-canvas.js:

TypeError: Drupal.offCanvas.$mainCanvasWrapper[0] is undefined

If you wonder why I disabled it - I tried to create a "Hello world" page instead of a <div><div><div>Hello World</div></div></div> page.

Steps to reproduce

* Disable off canvas wrapper via theme:

/**
 * Disable 'dialog-off-canvas-main-canvas' wrapper div
 */
function MY_THEME_element_info_alter(&$type) {
  if (!isset($type['page'])) return;
  unset ($type['page']['#theme_wrappers']['off_canvas_page_wrapper']);
}

* log in
* press "Annonuncements" in the top right of the toolbar

Proposed resolution

Disable wrapper resizing when the wrapper doesn't exist with adding if (!Drupal.offCanvas.$mainCanvasWrapper.length) return; to the top of bodyPadding() and resetPadding()

As a side effect this should solve (or add a workaround for) #3030690: Off-canvas js always add padding-right to the outer container? β†’ .

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Closed: won't fix

Version

11.0 πŸ”₯

Component
JavascriptΒ  β†’

Last updated about 2 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @raffaelj
  • Pipeline finished with Success
    4 months ago
    Total: 493s
    #111749
  • After sending a merge request, all tests are passed, but there is one warning I don't understand:

    If the last pipeline ran in the fork project, it may be inaccurate. Before merge, we advise running a pipeline in this project. Learn more

    I skimmed the linked help page, but it just looks like generic information about how to setup CI pipelines.

    Can I ignore it? If not, I need some guidance on how to resolve it.

  • Status changed to Needs review about 2 months ago
  • Maybe I get some attention after discovering the "Needs review" status update feature :-)

  • Status changed to Needs work about 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This may be border if it needs test coverage but the scenario makes me feel it may

    Changing to 11.x as the current development branch.

  • > Changing to 11.x as the current development branch.

    What's the recommended way to work with a changed dev branch? Should I merge the 11.x branch into my current issue fork or should I create a new issue fork?

    > This may be border if it needs test coverage but the scenario makes me feel it may

    OK. I have to learn, how to write JS tests for Drupal first. So this may take a while...

    ## Notes to myself

    ### Possible JS test solutions:

    * https://www.drupal.org/docs/develop/automated-testing/types-of-tests#s-f... β†’
    * https://www.drupal.org/docs/develop/automated-testing/browser-testing-us... β†’
    * https://www.drupal.org/docs/develop/automated-testing/javascript-testing... β†’

    ### Tutorial:

    * https://www.drupal.org/docs/automated-testing/phpunit-in-drupal/phpunit-... β†’

    ### Test cases

    * required modules: "announcements"
    * run test as authenticated user
    * run test on page, where the current theme is not the admin theme, e. g. `<front>`
    * either create click event on "Announcements" button before JS tests or run JS directly
    * test default setup with default theme (stable9 or stark should work) --> assert `document.querySelector('[data-off-canvas-main-canvas]') !== null`
    * dynamically inject theme, that disables `off_canvas_page_wrapper` via `MY_THEME_element_info_alter`
    * test default setup with injected theme --> assert JS error thrown
    * test changed behaviour with my fix --> assert `document.querySelector('[data-off-canvas-main-canvas]') === null` && assert no JS error thrown

  • Status changed to Closed: won't fix 18 days ago
  • πŸ‡ΊπŸ‡ΈUnited States bnjmnm Ann Arbor, MI

    This isn't really a a bug that requires fixing as is only happening when there is customization taking place, and it is something that can be mitigated with a little bit of additional customization.

    Create a library that depends on drupal.dialog.off_canvas then include a js file that overrides Drupal.offCanvas.bodyPadding and Drupal.offCanvas.resetPadding

    something like this would work:

    
    const oldBodyPadding = Drupal.offCanvas.bodyPadding;
    Drupal.offCanvas.bodyPadding = (event) => {
      if (!Drupal.offCanvas.$mainCanvasWrapper.length) {
         return
      };
      oldBodyPadding(event)
    
    }
    

    If this were impossible or noticeably difficult to address then categorizing as a bug would be appropriate, but in this case it is an issue that only occurs with a specific customization and can be fixed with not that much additional customization.

  • @bnjmnm Thanks for the workaround.

    This isn't really a a bug

    I would classify it as a bug, because Drupal is highly configurable and the script should check, if the node exists. But because I only noticed it in a fresh test installation, where the "announcements" module wasn't disabled already, I can live without the fix.

    Partially overwriting a core jQuery library, that probably gets replaced with a vanilla JS implementation, feels fragile. Also my goal was to reduce bloat and not to add more (JS) libraries.

    Now my "fix" is the advice to uninstall the announcements module when using my drinimal base theme. It's good enough for now...

Production build 0.69.0 2024