Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate Material Design JS?

import {MDCTextField} from '@material/textfield';

const textField = new MDCTextField(document.querySelector('.mdc-text-field'));
@import "@material/textfield/mdc-text-field";
<head>
  <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
  <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
</head>

<div class="mdc-text-field">
  <input type="text" id="my-text-field" class="mdc-text-field__input">
  <label class="mdc-floating-label" for="my-text-field">Hint text</label>
  <div class="mdc-line-ripple"></div>
</div>

I'm learning to work with Material Design. I thought it worked like bootstrap, meaning there is a CDN and then you just add the classes you need, so I got the CDN from this link: https://material.io/develop/web/docs/getting-started/

After I added the CDN I got the css working, but not JavaScript. In the instructions it says:

…and instantiate JavaScript:

mdc.ripple.MDCRipple.attachTo(document.querySelector('.foo-button'));

How do I instantiate Javascript?

I tried to put this code between script tags, but that didn't work. I think I'm missing some code here.

Update: The JS CDN seem to work but in each compenente I get an instruction for JavaScript Instantiation for example in this link: https://material.io/develop/web/components/input-controls/text-field/

import {MDCTextField} from '@material/textfield'; const textField = new MDCTextField(document.querySelector('.mdc-text-field'))

My question is where do i insert this code for the component to work.

like image 803
Vraja Avatar asked Dec 29 '19 19:12

Vraja


1 Answers

you need to add mdc.{component}.MDC{component} instead if you use cdn

const textField = new mdc.textField.MDCTextField(document.querySelector('.mdc-text-field'));
<head>
  <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
  <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
</head>

<label class="mdc-text-field mdc-text-field--filled">
  <span class="mdc-text-field__ripple"></span>
  <span class="mdc-floating-label" id="my-label-id">Hint text</span>
  <input class="mdc-text-field__input" type="text" aria-labelledby="my-label-id">
  <span class="mdc-line-ripple"></span>
</label>
like image 195
Aryqs Ipsum Avatar answered Oct 25 '22 17:10

Aryqs Ipsum