custom-element

How can a web-component get its own styles (width etc) as soon as possible?

女生的网名这么多〃 提交于 2019-12-07 00:37:30
Consider the following test-element web component definition (vanilla JavaScript running on Google Chrome, not Polymer), that creates a simple component with width=500px . Its attachedCallback function outputs its width to the console, and then sets up an asynchronous delay to do it again: test-element.html <style> test-element { display: inline-block; width: 500px; } </style> <script> (function (window, document) { var proto = Object.create(HTMLElement.prototype); proto.attachedCallback = function () { // Direct output. console.log("(1) test-element.width = ", this.offsetWidth); // Delayed

How can I dynamically change the shape of a clr-icon custom element?

安稳与你 提交于 2019-12-06 01:41:23
In the Clarity Icon docs they show that you can use the shape attribute to set the icons shape like this: <clr-icon shape="info-circle" size="16"></clr-icon> In my angular template I am using a clr-icon element like this: <clr-icon [shape]="myShape"></clr-icon> And use my component to set the string value of the shape bound to myShape : export class MyComponent { public myShape = 'volume-up'; changeShape() { if(this.myShape === 'volume-up') { this.myShape = 'volume-mute'; return; } this.myShape = 'volume-up; } } Using a button (not shown in the template) I want to run the changeShape() to

anchor tag <a id=“jump”> with hash inside shadow dom

拥有回忆 提交于 2019-12-05 12:20:47
I would like to use an anchor-element with a hash-URL inside of a custom element that uses shadow DOM. I would expect, that the browser scrolls down to that anchor, but it does not do it (at least Chrome). Detail: I have an index.html like this: ... <a href="#destinationInsideShadowDOM">Jump</a> ... <my-custom-element></my-custom-element> ... And another html-file for the custom-element, which contains the anchor: <template id="my-custom-element"> ... <a id="destinationInsideShadowDOM"></a> ... </template> I want the browser to scroll down to that anchor when I click on the link in index.html.

Vanilla Custom Element repeater for <option>, <li>, <td>

强颜欢笑 提交于 2019-12-05 00:30:45
问题 I'm currently trying to implement a repeater WebComponent to allow the company to easily create front-end without depending on any framework (decision took by architecture). Here's my current code: <ul> <company-repeat datas='[{"name": "NameValeur", "value": "valeurId"}, {"name": "NameObject", "value": "objectId"}]'> <li>${name}</option> </company-repeat> </ul> <select name="" id=""> <company-repeat datas='[{"name": "NameValeur", "value": "valeurId"}, {"name": "NameObject", "value": "objectId

Creating custom element without using class keyword

时光怂恿深爱的人放手 提交于 2019-12-04 18:39:00
问题 This is actually more a question about the object-orientation model in ES6. However I am going to use the creation of a new custom element as an example. So the new and shiny (as of today) method to create a new custom element is via customElements.define() which take in a tag name , a constructor , and options (which is optional) according to MDN, Google, and of course the spec. All the documentation listed uses a variation of the new class keyword for constructor . Assuming I don't like the

Extending <option>

偶尔善良 提交于 2019-12-04 12:53:45
I'm trying to extend the HTMLOptionElement , <template id="cOptionTemplate"> <content></content> </template> <select> <option is="custom-option">Test</option> </select> var cOption = document.registerElement('custom-option', { prototype: Object.create(HTMLOptionElement.prototype, { createdCallback: { value: function() { var template = document.getElementById("cOptionTemplate") var clone = document.importNode(template.content, true); this.createShadowRoot().appendChild(clone); } }, attributeChangedCallback: { value: function (attrName, oldVal, newVal){ switch(attrName){ case "attr1": console

document.registerElement - Why do we need to specify both 'prototype' and 'extends'?

老子叫甜甜 提交于 2019-12-04 11:20:23
Consider I want to extend the native button element, and create my own super-button element. As I know, it must follow the following pattern: var SuperButton = document.registerElement('super-button', { prototype: Object.create(HTMLButtonElement.prototype), extends: 'button' }); It looks strange to me - doesn't the prototype and extends parameters say the same thing? If I explicitly say that my super-button use the HTMLButtonElement prototype, why do I also need to specify that it extends the button element? isn't it redundant? For me it looks like exactly the same information. From the Custom

Adding CodeMirror to Shadow Dom of Custom Element?

纵饮孤独 提交于 2019-12-04 05:16:21
问题 I'd like to dynamically create a CodeMirror instance inside a Custom Element and have it live inside the element's Shadow DOM. For example: <code-mirror>foo</code-mirror> <script> window.customElements.define('code-mirror', class extends HTMLElement { constructor() { super(); let shadowRoot = this.attachShadow({mode: 'open'}); } connectedCallback() { this.cm = CodeMirror(this.shadowRoot, {lineNumbers: true}); } }); </script> This "works" but the layout is all wrong.. margin-left gets set to

Custom html tags on page render skip HTML parsing for some reason

允我心安 提交于 2019-12-04 04:29:54
问题 I don't know, why it's happening, but looks like custom html tags cannot parse it's content properly on page load if there's really a lot of such elements. document.registerElement('x-tag', { prototype: Object.create(HTMLElement.prototype, { attachedCallback: { value: function() { console.log(this.innerHTML, this.childNodes); // wrong innerHTML and childNodes once in n-occurrences } }}) } ); Here's an example My hypothesis is that there's some kind of stack, and sometimes this stack just

can i pass function as attribute to web component?

谁说胖子不能爱 提交于 2019-12-04 03:56:33
I'm trying to create a native web component for input element. I wanted the component to have custom validation functionality, similar to polymer's paper-input custom validator function. I'm not sure if I can pass a custom validator function as attribute to an instance of (web component) input element. Any suggestions would be appreciated. An attribute is a string, not a function. You can pass a a function as a string and then evaluate it with the eval() function. It's not considered as a good practice, for security reasons. Another solution is to pass it to the custom element as a Javascript