Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply MDL button style to a filePicker?

I would like to know if there is a way to apply the Material Design Lite button style to a file picker, i.e. a component created on an HTML page via:

<input type="file" id="filePicker" />

I would like the "Browse" button of the component to have the look of a Raised button (with ripple if possible). See http://www.getmdl.io/components/#buttons-section.

Thanks!

like image 578
Renaud Tarnec Avatar asked Oct 27 '25 10:10

Renaud Tarnec


2 Answers

Using CSS, do you mean something like this?

<style>#file { display: none }</style>

<input type="file" id="file">
<label for="file" class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored">
  <i class="material-icons">+</i>
</label>

https://jsfiddle.net/sj838cLg/

like image 180
cotorusso Avatar answered Oct 30 '25 06:10

cotorusso


Currently, there's no "official" way of doing that. According to the discussion on this issue, the reason is that there's no Material Design specification for that component, so they don't have a guideline to style it. On that same page, the user kybarg provided a CSS/JavaScript code to style a file picker which kind of matches the Material Design specification, so you can use that code:

HTML:

<form>
  <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
    <input class="mdl-textfield__input" type="text" />
    <label class="mdl-textfield__label">Name</label>
  </div>
  <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
    <input class="mdl-textfield__input" type="text"/>
    <label class="mdl-textfield__label">Position</label>
  </div>
  <div class="mdl-textfield mdl-js-textfield mdl-textfield--file">
    <input class="mdl-textfield__input" placeholder="File" type="text" id="uploadFile" readonly/>
    <div class="mdl-button mdl-button--primary mdl-button--icon mdl-button--file">
      <i class="material-icons">attach_file</i><input type="file" id="uploadBtn">
    </div>
  </div>
</form>

CSS:

html {
  width: 100%;
}
body {
  background: #f5f5f5;
  margin: 50px auto;
  width: 512px;
}

.mdl-button--file {
  input {
    cursor: pointer;
    height: 100%;
    right: 0;
    opacity: 0;
    position: absolute;
    top: 0;
    width: 300px;
    z-index: 4;
  }
}

.mdl-textfield--file {
  .mdl-textfield__input {
    box-sizing: border-box;
    width: calc(100% - 32px);
  }
  .mdl-button--file {
    right: 0;
  }
}

JavaScript:

document.getElementById("uploadBtn").onchange = function () {
    document.getElementById("uploadFile").value = this.files[0].name;
};

But as there's no official specification, you probably won't find an official implementation from the MDL team for now.

like image 20
cd1 Avatar answered Oct 30 '25 06:10

cd1