3

I am trying to access the individual parts of elements which you have to go through the shadow DOM. I have been trying to do it like in this guide: https://github.com/vaadin/vaadin-themable-mixin/wiki/1.-Style-Scopes

Adding this code to my shared-styles.html file works fine:

vaadin-text-field {
  border: 1px solid gray;
}

Then for accessing specific parts of the element to style, it shows to access it like this:

#shadow-root (open)
  <style>
    :host {
      border: 1px solid gray;
    }
  </style>

And that you can access the specific parts (input-field in this case) like this:

#shadow-root
  <style>
    [part="input-field"] {
      border: 1px solid gray;
    }
  </style>

I just do not understand how the last 2 code blocks are supposed to identify that they are changing 'vaadin-text-field' as it is nowhere in the code snippet. Adding those snippets alone of course do not work.. I feel like it's something simple I am overlooking. Thank you so much, I appreciate all of the great help available for Vaadin!

Nick Friskel
  • 2,369
  • 2
  • 20
  • 33
  • Updated URL for the above Style Scopes link is: https://github.com/vaadin/vaadin-themable-mixin#style-scopes. The content was moved from wiki to readme. Currently the old wiki pages still link forward to the readme, but the wiki pages might get removed in the future. – Haprog Aug 30 '19 at 09:06

2 Answers2

6

Check the Stylable Shadow Parts section of the docs. It gives some examples that would be helpful.

Shortly, to style a Vaadin component (like <vaadin-text-field>) you would need to add your styles into the shadow DOM of the component. In order to do that define a style module with a theme-for="vaadin-text-field" attribute and place your custom styles there. Before the first render Vaadin components look for style modules with the theme-for attribute and pick styles from there.

<dom-module id="my-input-theme" theme-for="vaadin-text-field">
  <template>
    <style>
      [part="input-field"] {
        border: 1px solid gray;
      }
    </style>
  </template>
</dom-module>

<vaadin-text-field label="Username"></vaadin-text-field>

Here is a live example: https://glitch.com/edit/#!/theming-vaadin-components-npm-p3?path=custom-theme.js:4:0

The example above would change the styles of all <vaadin-text-field>s in the app. If you want to style only some text fields, use a theme or an attribute selector:

<dom-module id="my-input-theme" theme-for="vaadin-text-field">
  <template>
    <style>
      :host([theme~="bordered"]) [part="input-field"] {
        border: 3px solid hotpink;
      }
    </style>
  </template>
</dom-module>

<vaadin-text-field label="Themed" theme="bordered"></vaadin-text-field>
Viktor Lukashov
  • 338
  • 1
  • 6
  • Thanks! I'm trying to get the last part to work, but i'm writing this in all Java and there is no method to set 'theme' to a specific button. The first example is working for all buttons, but how would I set the specific button theme in java? thanks for the help! – Nick Friskel Apr 09 '18 at 18:08
  • 1
    Vaadin 10 has APIs for almost any kind of DOM manipulation from server-side Java. In order to set an attribute on a `TextField` component, first access it's corresponding DOM element with the `getElement()` method, and then use the Element API to set an attribute: `textField.getElement().setAttribute("theme", "bordered");` More details in the docs: [working with UI components](https://vaadin.com/docs/v10/flow/components/tutorial-component-basic-features.html#element), [the Element API](https://vaadin.com/docs/v10/flow/element-api/tutorial-properties-attributes.html). – Viktor Lukashov Apr 09 '18 at 19:40
  • Thank you so much, this is exactly what i needed. If you wouldn't mind answering another question, is there a way to programatically set the background color of a vaadin-grid row based on something inside of it? like if a value is positive, set it to green, if negative, set row background to red. Not sure how I would go about doing that – Nick Friskel Apr 09 '18 at 22:37
  • 1
    Happy to help! Would you mind posting your other question separately from this one? – Viktor Lukashov Apr 10 '18 at 04:12
  • Thank you so much Viktor! I have added the question here https://stackoverflow.com/questions/49747059/vaadin-10-grid-style-individual-row-based-on-contents – Nick Friskel Apr 10 '18 at 06:43
  • Updated URL for the above "Stylable Shadow Parts" link is: https://github.com/vaadin/vaadin-themable-mixin#stylable-shadow-parts. The content was moved from wiki to readme. Currently the old wiki pages still link forward to the readme, but the wiki pages might get removed in the future. – Haprog Aug 30 '19 at 09:08
  • Thank you very much! The part to style only one specific component with the attribute selector helped me alot! – xela84 Jan 24 '20 at 10:46
1

The last examples would require you to actually edit the contents of the style-tag inside the component.

At the bottom of that page you'll find a Read next: Adding Styles to Local Scope.

Erik Lumme
  • 5,202
  • 13
  • 27
  • Updated URL for the above "Adding Styles to Local Scope" link is: https://github.com/vaadin/vaadin-themable-mixin#adding-styles-to-local-scope. The content was moved from wiki to readme. Currently the old wiki pages still link forward to the readme, but the wiki pages might get removed in the future. – Haprog Aug 30 '19 at 09:07