-
Notifications
You must be signed in to change notification settings - Fork 4
NoScriptProperty
Detect if javascript is enabled/disabled using CSS variables
This component takes advantage of fallback values on CSS variables to conditionally render a style, see examples
This component is useful when you want to conditionally style elements depending on if javascript is enabled/disabled, comes with a class by default that hides elements using display: none
if javascript is disabled
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty />
<div class="noscript-hide">
This element is hidden if javascript is disabled
</div>
<p style="color: var(--noscript, red);">
This text is only red if javascript is disabled
</p>
Default: :root
The CSS selector that the variable is attached to, not recommended to change unless scoping the variable to a specific element
Default: noscript
--{name}
Name of the included conditional CSS property, its value is equal to default
when javascript is enabled or initial
if javascript is disabled
Note: Be careful that this value does not collide with other CSS variables on your website
Default: {name}-hide
Name of the included class that will hide an element using display: none
if javascript is disabled
Pass false
to stop class from being added
Class is only included if the default
prop is false
Note: Be careful that this value does not collide with other CSS classes on your website
Default: false
Default value of the --{name}
CSS property (when javascript is enabled), defaults to false
for the included hide class
Anything passed to default prop will be passed into a <noscript>
element
<noscript>
...
<slot />
</noscript>
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty />
<div class="noscript-hide">
This element is hidden if javascript is disabled
</div>
Note: Make sure this class does not collide with any class names in your project
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty class="no-js-hide"/>
<div class="no-js-hide">
This element is hidden if javascript is disabled
</div>
Exclude the included class from being created if you only want to use the CSS variable
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty class={false}/>
<p style="color: var(--noscript, red);">
This text is only red if javascript is disabled
</p>
Note: Make sure this variable name does not collide with any other variables in your project
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty name="no-js"/>
<p style="color: var(--no-js, red);">
This text is only red if javascript is disabled
</p>
Change the default
value (value passed when javascript is enabled) of CSS variable
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty default="green"/>
<p style="color: var(--noscript, yellow);">
This text is green if javascript is enabled, yellow if javascript is disabled
</p>
Only for advanced use cases
---
import { NoScriptProperty } from 'astro-headless-ui';
---
<NoScriptProperty selector="#one" name="noscript-1" default="green"/>
<NoScriptProperty selector="#two" name="noscript-2" default="blue"/>
<section id="one">
<p style="color: var(--noscript-1, yellow);">
This text is green if javascript is enabled, yellow if javascript is disabled
</p>
</section>
<section id="two">
<p style="color: var(--noscript-2, red);">
This text is blue if javascript is enabled, red if javascript is disabled
</p>
</section>