GithubHelp home page GithubHelp logo

Comments (12)

defnotrobbie avatar defnotrobbie commented on July 18, 2024 27

i'd just like to add that the following works for both adding and removing inert in React without using refs or DOM methods. note that the true assignment is empty string and the false assignment is null. the true assignment can be any string as far as i can tell. the false assignment can be null, undefined, or false.

<div inert={inertCondition ? '' : null} />

from inert.

pimterry avatar pimterry commented on July 18, 2024 15

Just to extend this a little, if you want conditional inertness in React you need something more like:

<div
  ref={node => node && (shouldBeInert ?
    node.setAttribute('inert', '') : node.removeAttribute('inert')
  )}
/>

from inert.

LarsEjaas avatar LarsEjaas commented on July 18, 2024 7

If anyone needs a workaround until this is implemented I have solved it like this:

<div inert={disabled? "" : undefined}

inert undefined will not pass inert to the underlying DOM element and inert="" will pass to the DOM node and works like inert={true}

then:

declare global {
//TODO: remove this when inert is supported in React types
  namespace JSX {
    interface IntrinsicAttributes {
      /**
       * Boolean attribute indicating that the browser will ignore the element.
       *
       * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert MDN Web Docs}
       */
      inert?: "";
    }
  }
}

from inert.

robdodson avatar robdodson commented on July 18, 2024

Closing this as it's a React specific issue and I just wanted to document the workaround. You can see the discussion on React allowing custom attributes/properties here: facebook/react#140

from inert.

robdodson avatar robdodson commented on July 18, 2024

Thank you Tim!

from inert.

panmona avatar panmona commented on July 18, 2024

I am receiving node.setAttribute is not a function when I try to use this. Any idea on how to solve this?

from inert.

bkardell avatar bkardell commented on July 18, 2024

@maracuja-juice you're getting this following @robdodson's example ?

hey @robdodson - the link above showing a functional snip at https://www.webpackbin.com/bins/-KmmTaOzo6tSQqTeIv2H appears to be gone

from inert.

panmona avatar panmona commented on July 18, 2024

@bkardell I'm using pimterry's example but I guess I will have the exact same behaviour if I would use the original suggestion as they're not that different.

from inert.

bkardell avatar bkardell commented on July 18, 2024

Can you share any running code? Can you add a debugger and see what node is there?

from inert.

robdodson avatar robdodson commented on July 18, 2024

Hm this sounds like an issue with how @maracuja-juice is using refs in React. I'm not super familiar with React but maybe in newer versions the ref syntax has changed?

from inert.

miloxeon avatar miloxeon commented on July 18, 2024

If you're by any chance using React with TypeScript and Linaria, do this:

const InertElement = styled.div<{ inert?: string | null }>`
   /* your styles here */
`

Then, as @defnotrobbie suggested, you can use inert conditionally, and TypeScript will still be happy because we defined an additional prop:

<InertElement inert={inertCondition ? '' : null} />

from inert.

yangshun avatar yangshun commented on July 18, 2024

Declaring on namespace JSX did not work for me, but this did:

declare module 'react' {
  interface HTMLAttributes<T> extends DOMAttributes<T> {
    /**
     * Boolean attribute indicating that the browser will ignore the element.
     *
     * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inert MDN Web Docs}
     */
    inert?: '';
  }
}

from inert.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.