GithubHelp home page GithubHelp logo

alterebro / auto-resize-custom-select Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 537 KB

Auto resize drop-down select elements to match the width of the selected option and customize it with CSS

Home Page: https://alterebro.github.io/auto-resize-custom-select/

License: MIT License

JavaScript 100.00%
select autoresize dropdown custom css option selector customselect

auto-resize-custom-select's Introduction

auto-resize-custom-select # npm

Custom Select

MIT license NPM Version File Size Twitter

Auto-resize the width of a select element based on the size of the current selected option. Fully customizable with CSS without the common styling limitations of a select element.

customSelect. Auto resizes drop-down select html elements to fit the size of the option that has been selected. It also gives you total freedom and control to style the list selector with CSS without the usual constraints on this form elements. It creates a substitute span element with the .custom-select class, which contains two children span elements, with the selection value (.selection) and the expand figure (.arrow) that you can stylize as you wish.

<!-- Generated markup from the <select/> element -->
<span class="custom-select">
    <span class="selection">value</span>
    <span class="arrow">[::after]</span>
</span>

Install

$ npm i auto-resize-custom-select

Easiest way to install it is via NPM or including the minified file from the unpkg or jsdelivr CDNs.

<script src="https://unpkg.com/auto-resize-custom-select"></script>
<script src="https://cdn.jsdelivr.net/gh/alterebro/auto-resize-custom-select/dist/customSelect.min.js"></script>

Examples

Some examples can be seen on this CodePen: https://codepen.io/alterebro/full/RwopMYp

Custom Select Examples

Usage

Just include it on your document and call the customSelect function. The script is available as ES5 minified file and as ES Module.

    ...
    <select>
        <option value="1">un</option>
        <option value="2">deux</option>
        <option value="3">trois</option>
    </select>
    ...
    <script src="https://unpkg.com/auto-resize-custom-select"></script>
    <script>
        customSelect();
    </script>
</body>
</html>
import customSelect from 'auto-resize-custom-select';

customSelect();

Options

You can set some options by passing an object as parameter to overwrite the default values:

customSelect({
    el: 'select',
    className: 'custom-select',
    expandChar: '\u25BE',
    width: false,
    injectCSS : true
});

el

A css selector that'll pick the elements you want to be affected by the script. It defaults to select in order to take all the &lt;select&gt; elements.

className

Name of the class that will adopt the substitute parent element. Its default name is custom-select. The element tree to style then result as follows:

.custom-select {} /* Container Element */
.custom-select .selection {} /* span containing the selected option */
.custom-select .arrow {} /* span containing the expand element (arrow) */
.custom-select .arrow:after {} /* - it's created using the :after pseudo-element */

expandChar

The expand character created via content in the :after pseudo-element. The default value is the CSS ISO Code \u25BE ( Black down-pointing small triangle )

width

When set to false (default) it will resize itself to the width of the selected option. When set to a valid css dimension value, it will have a fixed size.

injectCSS

The script injects by default some CSS styles at the top of the head of your document in order to create a minimum acceptable design for the generated code. If you don't need it and you want to style it your way, this can be disabled by setting this property to false

/* Default injected CSS code: */
.custom-select {
    position: relative;
    display: inline-flex;
    align-items: center;
}
.custom-select .selection {
    text-decoration: underline;
    flex: 1 1 auto;
}
.custom-select .arrow:after {
    content: '\u00a0\u25BE'; /* No-break Space + Expand Char */
}
.custom-select select {
    all: revert;
    position: absolute;
    cursor: pointer;
    opacity: 0;
}

Development

# Source: /src/customSelect.js

# Clone the repo
$ git clone https://github.com/alterebro/auto-resize-custom-select.git
$ cd auto-resize-custom-select/

# Install dependencies
$ npm install

# Build (/dist)
$ npm run build

# Build (/docs: https://alterebro.github.io/auto-resize-custom-select/)
$ npm run docs

License

MIT © Jorge Moreno. @alterebro

auto-resize-custom-select's People

Contributors

alterebro avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.