Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Border Color and Border Style Generation to provide individual edge utilities #560

Closed
wants to merge 3 commits into from
Closed

Conversation

spiltcoffee
Copy link
Contributor

Fixes #559.

I was bored so I tried making a fix for this change. I'm not sold on this implementation, though - this increases the size of border color utilities fourfold, and they may not all be used, so as a default it's proooooobably not the best thing.

If this was more customisable, it might be better, but I'm thinking it might be better to try and do this as a plugin instead.

@tlgreg
Copy link

tlgreg commented Sep 29, 2018

This did came up in the past, not sure if things changed since.
tailwindlabs/discuss#46

@spiltcoffee
Copy link
Contributor Author

Yeah, probably not. I'm thinking this pull request should be rejected, considering how much data it would add to the default case.

It's probably better done as a plugin for those wanting these styles. For those who aren't sure how, here's an attempt at an implementation you can use: https://github.com/spiltcoffee/tailwindcss/commit/e3f41fb746b1f1388cde2a4bd44f5b4d9f0b0d55

(If there's demand, I could clean this up into it's own repo so you can install it through npm).

@simensen
Copy link

The following appears to work just fine for people who are wanting to know how to create this plugin.

var _ = require('lodash')
var flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').default

module.exports = {
  plugins: [
    function({ addUtilities, e, theme, variants }) {
      const colors = flattenColorPalette(theme('borderColor'))

      const utilities = _.flatMap(_.omit(colors, 'default'), (value, modifier) => ({
        [`.${e(`border-t-${modifier}`)}`]: { borderTopColor: `${value}` },
        [`.${e(`border-r-${modifier}`)}`]: { borderRightColor: `${value}` },
        [`.${e(`border-b-${modifier}`)}`]: { borderBottomColor: `${value}` },
        [`.${e(`border-l-${modifier}`)}`]: { borderLeftColor: `${value}` },
      }))

      addUtilities(utilities, variants('borderColor'))
    },
  ],
}

@icaroscherma
Copy link

@simensen Great job! I changed a bit your code for people that don't want to use Lodash just for this.

var flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').default;

module.exports = {
  plugins: [
    ({ addUtilities, e, theme, variants }) => {
      const colors = flattenColorPalette(theme('borderColor'));
      delete colors['default'];

      const colorMap = Object.keys(colors)
        .map(color => ({
          [`.border-t-${color}`]: {borderTopColor: colors[color]},
          [`.border-r-${color}`]: {borderRightColor: colors[color]},
          [`.border-b-${color}`]: {borderBottomColor: colors[color]},
          [`.border-l-${color}`]: {borderLeftColor: colors[color]},
        }));
      const utilities = Object.assign({}, ...colorMap);

      addUtilities(utilities, variants('borderColor'));
    },
  ],
};

@CristianDeluxe
Copy link

@simensen and @icaroscherma Awesome job!!, just my 2 cents in case someone is using extended custom colors.

var flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').default;

module.exports = {
  plugins: [
          ({ addUtilities, e, theme, variants }) => {
              let colors = flattenColorPalette(theme('borderColor'));
              delete colors['default'];
  
              // Replace or Add custom colors
              if(this.theme?.extend?.colors !== undefined){
                  colors = Object.assign(colors, this.theme.extend.colors);
              }
  
              const colorMap = Object.keys(colors)
                  .map(color => ({
                      [`.border-t-${color}`]: {borderTopColor: colors[color]},
                      [`.border-r-${color}`]: {borderRightColor: colors[color]},
                      [`.border-b-${color}`]: {borderBottomColor: colors[color]},
                      [`.border-l-${color}`]: {borderLeftColor: colors[color]},
                  }));
              const utilities = Object.assign({}, ...colorMap);
  
              addUtilities(utilities, variants('borderColor'));
          },
      ],
};

@tomchen
Copy link

tomchen commented Mar 11, 2021

Border opacity does not work with this plugin method, otherwise it works fine

@rjworks
Copy link

rjworks commented May 2, 2021

Border opacity does not work with this plugin method, otherwise it works fine

how did you use it?

@rjworks
Copy link

rjworks commented May 2, 2021

Border opacity does not work with this plugin method, otherwise it works fine

how did you use it?

nvm!
border-r-red-900 border-l-blue-900 bg-blue-600

@mercedesb mercedesb mentioned this pull request Jun 2, 2021
1 task
@eswat2
Copy link

eswat2 commented Jun 19, 2021

Exactly what I was looking for to finish my port... !!

@hellomrbigshot
Copy link

Nice! It works for me.

@pwkurth
Copy link

pwkurth commented Jun 26, 2023

Might be solved with this PR

#4404

@icaroscherma
Copy link

@pwkurth yes, this is old school, now you can just set a custom color on tailwind.config.js under theme.extend.colors.YOUR_COLOR_NAME and it will be available everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Individual Border Settings
10 participants