nuka-carousel icon indicating copy to clipboard operation
nuka-carousel copied to clipboard

Narrow the spacing between dots

Open brandon-arnold opened this issue 10 years ago • 3 comments

I am finding the documentation for the decorators property lacking. How can I reduce the padding between dots, as it is done on the nuka-carousel demo website? In my application they're spaced out to some 30px apart and wrapping onto multiple lines of 2.

brandon-arnold avatar Nov 08 '16 19:11 brandon-arnold

The decorators are just an array of components that you can style any way that you want. By default they are populated with the decorators defined in the nuka-carousel src file, but you can replace them with your own decorators if or update the existing ones.

If you look in node_modules/nuka-carousel/src/decorators you'll see how they are doing it. What I did was copy the code from that file into my own decorators file, and then used that in my component.

brybrophy avatar Nov 16 '16 04:11 brybrophy

Here is what the default decorators look like. I made comments throughout to guide you to where the padding is being set.

'use strict';

import React from 'react';

// An array of React components. There are 3. The first 2 are
//  the prev/next buttons, and the  3rd one is the dots.
const DefaultDecorators = [
  {
    component: React.createClass({
      render() {
        return (

// This button is the previous button on the left hand side
          <button
 // It is being styled by a function, getButtonStyles. You'll see it down below
            style={this.getButtonStyles(this.props.currentSlide === 0 && !this.props.wrapAround)}
            onClick={this.handleClick}>PREV</button>
        )
      },
      handleClick(e) {
        e.preventDefault();
        this.props.previousSlide();
      },
// Here is where the styles are coming from. Go to the last
// component in the array to find the style for the dots.
      getButtonStyles(disabled) {
        return {
          border: 0,
          background: 'rgba(0,0,0,0.4)',
          color: 'white',
          padding: 10,
          outline: 0,
          opacity: disabled ? 0.3 : 1,
          cursor: 'pointer'
        }
      }
    }),
    position: 'CenterLeft'
  },
  {
    component: React.createClass({
      render() {
        return (
          <button
            style={this.getButtonStyles(this.props.currentSlide + this.props.slidesToScroll >= this.props.slideCount && !this.props.wrapAround)}
            onClick={this.handleClick}>NEXT</button>
        )
      },
      handleClick(e) {
        e.preventDefault();
        this.props.nextSlide();
      },
      getButtonStyles(disabled) {
        return {
          border: 0,
          background: 'rgba(0,0,0,0.4)',
          color: 'white',
          padding: 10,
          outline: 0,
          opacity: disabled ? 0.3 : 1,
          cursor: 'pointer'
        }
      }
    }),
    position: 'CenterRight'
  },
  {
    component: React.createClass({
      render() {
        var self = this;
        var indexes = this.getIndexes(self.props.slideCount, self.props.slidesToScroll);
        return (
          <ul style={self.getListStyles()}>
            {
              indexes.map(function(index) {
                return (
// Here are the dots. You can see that they are <li> elements 
// within a <ul> element. The <li>'s each have a button inside.
// Down below you'll find the getButtonStyles() function.
                  <li style={self.getListItemStyles()} key={index}>
                    <button
                      style={self.getButtonStyles(self.props.currentSlide === index)}
                      onClick={self.props.goToSlide.bind(null, index)}>
                      &bull;
                    </button>
                  </li>
                )
              })
            }
          </ul>
        )
      },
      getIndexes(count, inc) {
        var arr = [];
        for (var i = 0; i < count; i += inc) {
          arr.push(i);
        }
        return arr;
      },
      getListStyles() {
        return {
          position: 'relative',
          margin: 0,
          top: -10,
          padding: 0
        }
      },
      getListItemStyles() {
        return {
          listStyleType: 'none',
          display: 'inline-block'
        }
      },
      getButtonStyles(active) {
        return {
          border: 0,
          background: 'transparent',
          color: 'black',
          cursor: 'pointer',
// Here is your padding. 
          padding: 10,
          outline: 0,
          fontSize: 24,
          opacity: active ? 1 : 0.5
        }
      }
    }),
    position: 'BottomCenter'
  }
];

export default DefaultDecorators;

brybrophy avatar Nov 16 '16 04:11 brybrophy

closed in favor of brybrophy's answer. Thanks for the input!

mdaxtman avatar Nov 18 '16 00:11 mdaxtman