tailwindlabs / tailwindcss-custom-forms

A better base for styling form elements with Tailwind CSS.
https://tailwindcss-custom-forms.netlify.app/
MIT License
1.55k stars 72 forks source link

Add support for indeterminate checkboxes #86

Closed adamwathan closed 4 years ago

adamwathan commented 4 years ago

This PR adds default styles for indeterminate checkboxes using the :indeterminate selector, which can be triggered by setting element.indeterminate = true on the actual checkbox node using JavaScript.

Implementing this in a sensible way requires a breaking change that no longer lets you set the icon or iconColor for checkboxes/radios directly under the root checkbox or radio key. You always have to set it under &:checked now.

module.exports = {
  theme: {
    extend: {
      customForms: {
        DEFAULT: {
          css: {
            // Before
            checkbox: {
              iconColor: 'pink',
            },

            // Now
            checkbox: {
              '&:checked': {
                iconColor: 'pink',
              },
            },
          },
        },
      },
    },
  },
}

Customizing the indeterminate icon stuff is done the same way as with :checked:

module.exports = {
  theme: {
    extend: {
      customForms: {
        DEFAULT: {
          css: {
            checkbox: {
              '&:indeterminate': {
                iconColor: 'pink',
              },
            },
          },
        },
      },
    },
  },
}
swanson commented 4 years ago

Okay so I pulled down this branch to test. It does work fine with the pseudo-selector if you use JS to set indeterminate, but it does NOT work if you use the HTML attribute. I think setting it via JS is much more likely (for use as a bulk selection input).

I think the MDN docs were slightly misleading as further down the page there is this snippet:

This is set using the HTMLInputElement object's indeterminate property via JavaScript (it cannot be set using an HTML attribute)

So it appears that the pseudo-selector is the right way to go and we should not worry about the HTML attribute version.