11

Sveltekit has very strict a11y checks, for instance you can't just add on:click to a div.

I can suppress it on a per-line bases, e.g.:

<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div on:click={handleClick}></div>

But how can I disable it globally?

2
  • 2
    Your third reason is not valid, buttons can have any size, it does not matter and the style is completely flexible. Just use a button. Commented Nov 20, 2023 at 19:42
  • some members of sveltejs team think a11y is political correctness, although a11y is not important for your project, in the case, these warnings just torment developers Commented Jun 26, 2024 at 2:21

6 Answers 6

21

In sveltekit, a11y warnings nag you in several places.

1. squiggles in VSCode

In .vscode/settings.json, add:

"svelte.plugin.svelte.compilerWarnings": {
  "a11y-click-events-have-key-events": "ignore",
  "a11y-no-static-element-interactions": "ignore"
}

2. eslint

If your build steps include running eslint in the CLI, the build can fail.

Customize this rule in your eslint config:

"rules": {
  "svelte/valid-compile": ["error", { "ignoreWarnings": true }]
}

This will suppress non-fatal errors reported by the valid-compile rule, which is part of plugin:svelte/recommended.

3. svelte-check

If your build steps include running svelte-check in the CLI, the build can fail.

See if you're running svelte-check with the --fail-on-warnings flag. If yes, remove that flag so the warning will still show on screen, but won't raise an error.

This was quick and easy, but your codebase could deteriorate overtime, because all warnings are ignored, especially if the build pipeline runs remotely and nobody stares at the stdout. A better way is to disable things individually:

svelte-check --fail-on-warnings --compiler-warnings "a11y-click-events-have-key-events:ignore,a11y-no-static-element-interactions:ignore"

This is better but quite verbose, and pollutes your package.json. Personally, I choose to extract it to a svelte-check.sh, and call it from package.json:

"scripts": {
  ...
  "check": "bash svelte-check.sh",
  ...
}

4. vite dev and vite build

Both will show warnings on screen, but still work fine. No build pipelines will fail, so there isn't a necessity to change.

But if you still want to get rid of the warnings, add this in svelte.config.js:

const config = {
  onwarn: (warning, handler) => {
    // suppress warnings on `vite dev` and `vite build`; but even without this, things still work
    if (warning.code === "a11y-click-events-have-key-events") return;
    if (warning.code === "a11y-no-static-element-interactions") return;
    handler(warning);
  },
  kit: { adapter: adapter() },
};

Some people found they also needed to add similar things in vite.config.js, but I didn't find it necessary.

Sign up to request clarification or add additional context in comments.

5 Comments

Just a small note for copy/pasters like me: "ignoreWarnings": false should actually be "ignoreWarnings": true if you want eslint to leave you alone
@ryanovas you're right! I edited.
It seems that only warnings at build time can be suppressed
I do not know why but in my case, I had to use underscore "a11y_click_events_have_key_events" instead of hyphen "a11y-click-events-have-key-events".
it's 2024 and it seems the right place to put the error handling is in compilerOptions.warningFilter and use underscores instead of dashes as innomatic mentioned above.
9

I added warningFilter to svelte.config.js. And it worked.

const config = {
    kit: {
        adapter: adapter()
        ...
    },
    compilerOptions: {
        warningFilter: (warning) => {
            const ignore = [
                'a11y_media_has_caption',
                'a11y_no_redundant_roles',
                'a11y_consider_explicit_label',
                'a11y_no_noninteractive_tabindex',
                'a11y_click_events_have_key_events',
                'a11y_no_static_element_interactions',
                'a11y_no_noninteractive_element_interactions',
            ]
            return !ignore.includes(warning.code)
        },
    }
};

4 Comments

I tried all of the other suggestions on this page and this is the only one that worked with the latest versions of Svelte/SvelteKit as of January 2025.
Only this solution will work with svelte kit 2.0 and svelte 5.0 in march 2025
agreed, this is what works for svelte as of April 2025 "@sveltejs/kit": "^2.16.0", "@sveltejs/vite-plugin-svelte": "^5.0.0", "svelte": "^5.0.0", "svelte-check": "^4.0.0", "typescript": "^5.0.0", "vite": "^6.2.5"
Can be made more generic with warningFilter: (warning) => !warning.code.startsWith("a11y_").
4

svelte lang-tools developers think it is necessary, this is an issue: https://github.com/sveltejs/language-tools/issues/650

You can add a property role="presentation" or role="none" to the element.

It removes an element's implicit ARIA semantics from being exposed to the accessibility tree.

ref: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/presentation_role

1 Comment

That one is working. I think this is the proper way to deal with the issue. Tried configs but they stopped working for me.
2

to add to @ZYinMD answer for #4 in svelte.config.js


const config = {
    onwarn: (warning, handler) => {
        if (warning.code.startsWith('a11y-')) return;
        handler(warning);
    }
};

1 Comment

Probably not a good idea to suppress all a11y warnings. This may work but it's bad practice.
1

To disable almost all the blue warning in sveltekit, do this

open setting.json in vscode and add this

"svelte.plugin.svelte.compilerWarnings": {
        "css-unused-selector": "ignore",
        "unused-export-let": "ignore",
        "a11y-aria-attributes": "ignore",
        "a11y-incorrect-aria-attribute-type": "ignore",
        "a11y-unknown-aria-attribute": "ignore",
        "a11y-hidden": "ignore",
        "a11y-autocomplete-valid": "ignore",
        "a11y-misplaced-role": "ignore",
        "a11y-no-static-element-interactions": "ignore",
        "a11y-unknown-role": "ignore",
        "a11y-no-abstract-role": "ignore",
        "svelte-ignore a11y-autofocus": "ignore",
        "a11y-no-redundant-roles": "ignore",
        "a11y-role-has-required-aria-props": "ignore",
        "a11y-accesskey": "ignore",
        "a11y-autofocus": "ignore",
        "a11y-misplaced-scope": "ignore",
        "a11y-positive-tabindex": "ignore",
        "a11y-invalid-attribute": "ignore",
        "a11y-missing-attribute": "ignore",
        "a11y-img-redundant-alt": "ignore",
        "a11y-label-has-associated-control": "ignore",
        "a11y-media-has-caption": "ignore",
        "a11y-distracting-elements": "ignore",
        "a11y-structure": "ignore",
        "a11y-mouse-events-have-key-events": "ignore",
        "a11y-missing-content": "ignore",
        "a11y-click-events-have-key-events": "ignore",
        "a11y-no-noninteractive-element-interactions": "ignore"
    },

Then open the svelte.config.js file and do this

const config = {
    preprocess: vitePreprocess(),
    onwarn: (warning, handler) => {
        if (warning.code.startsWith('a11y-')) return
        if (warning.code === 'missing-exports-condition') return
        if (warning.code === 'a11y-no-static-element-interactions') return
        if (warning.code === 'svelte-ignore a11y-autofocus') return
        if (warning.code.startsWith('css-unused-selector')) return
        handler(warning)
    },
    kit: {
        adapter: adapter(),
    },
}

Add more rules you hate into those files/config.

Comments

0

https://github.com/sveltejs/language-tools/issues/650#issuecomment-1806928309

sed -i 's/warn(pos, warning) {$/\0 if (warning.code.includes("a11y")) { return; }/' node_modules/svelte/compiler.cjs  

This worked for me. For those under Windows:

# Execute this under your project root
(Get-Content -Path "node_modules\svelte\compiler.cjs") -replace 'warn\(pos, warning\) {\$', '$& if (warning.code.includes("a11y")) { return; }' | Set-Content -Path "node_modules\svelte\compiler.cjs"

Ctrl-P -> Restart Svelte Language Server after patching the code

A11y is a good feature, troublesome sometimes, however. There should be some way that isn't that tricky for devs to disable it.

Btw any solutions other than this don't work for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.