Skip to content

Fluent HTML component API? #486

Open

Description

@Legioth

We should evaluate whether the benefits of a fluid api for the HTML components would outweigh the inconsistencies caused by overriding.

Activity

  1. denis-anisimov commented on Apr 21, 2016

    @denis-anisimov
    Contributor

    The test added to #555 shows that there will be no way to implement fluent API for components "correctly" (make component parameterized so that setters return parameter type not current one).
    So

    public class Div<D extends Div> extends HtmlComponent {
    
       public D setWhatever(){
       }
    }

    won't work.

    So it's better to use Builders instead.

  2. macjuan commented on Apr 27, 2016

    @macjuan
    Contributor

    Added to the backlog

  3. Legioth commented on Apr 27, 2016

    @Legioth
    MemberAuthor

    Affects #495 and #494

  4. svenruppert commented on Feb 11, 2019

    @svenruppert

    There is a way to add a Fluent API, completely out of the inheritance. The same could be used to add a DSL for Kotlin later ( additionally dependency). I created the solution and it looks like the following...

      private final VerticalLayout layout = new VerticalLayoutBuilder(VerticalLayout::new)
          .setDefaultHorizontalComponentAlignment(Alignment.START)
          .setSizeUndefined()
          .add(new HorizontalLayout(username , password))
          .add(rememberMe)
          .add(new HorizontalLayout(btnLogin , btnCancel))
          .build();
    
    private final Button btnCancel = new ButtonBuilder(Button::new)
          .setId(BTN_CANCEL_ID)
          .addClickListener(e -> clearFields())
          .setText(getTranslation(BTN_CANCEL_CAPTION))
          .setVisible(true)
          .build();
    
  5. pleku commented on Feb 11, 2019

    @pleku

    Thanks for the effort @svenruppert ! but personally, I would hate having to write the following to instantiate something

    new VerticalLayoutBuilder(VerticalLayout::new)

    and I would never use this. There needs to be a simpler way. (less is more)

    And as someone commented on our Slack, this would be a good thing to pitch as an add-on. IMO not part of @mstahv's Viritin, but something separate so that it would be possible to see how many people opt to use it. Viritin has bunch of other stuff too that make it harder to see what features should be promoted as official framework features.

  6. svenruppert commented on Feb 11, 2019

    @svenruppert

    ok

  7. Legioth commented on Feb 12, 2019

    @Legioth
    MemberAuthor

    A builder API could be made in a way that typically doesn't require the new VerticalLayoutBuilder(VerticalLayout::new) step, even though it would be kept there for the really advanced cases. Instead, there could be static builder factory methods in the class itself. In that way, the examples could be changed to look like this:

    private final Button btnCancel = Button.builder(getTranslation(BTN_CANCEL_CAPTION))
          .setId(BTN_CANCEL_ID)
          .addClickListener(e -> clearFields())
          .setVisible(true)
          .build();
    
     private final VerticalLayout layout = VerticalLayout.builder()
          .setDefaultHorizontalComponentAlignment(Alignment.START)
          .setSizeUndefined()
          .add(new HorizontalLayout(username , password))
          .add(rememberMe)
          .add(new HorizontalLayout(btnLogin , btnCancel))
          .build();
    

    The biggest challenge in any way is that we would have to ensure the builder classes are always in sync with the actual components. It might be possible to use some kind of code generation library for automating this, but I'm not too familiar with such things.

  8. svenruppert commented on Feb 12, 2019

    @svenruppert

    Sure, the solution from me was assuming that there is no change in the existing framework and to support extended Components.

  9. svenruppert commented on Feb 12, 2019

    @svenruppert

    To keep them in sync, I started working on an AnnotationProcessor. It is not too difficult.. With this approach, we could add support for custom components as well. (maybe a Pro)

  10. jhult commented on Feb 22, 2020

    @jhult
    Contributor

    Just leaving this here as a plug for a great existing Kotlin DSL for Vaadin:

  11. Frettman commented on Nov 28, 2024

    @Frettman

    Is there any gauge on how requested a fluent API still is?
    I initially created one as kind of an experimental "bonus" in another add-on and wouldn't want to miss it anymore. I'm currently putting the finishing touches on moving it to its own project(s) where most of it is automatically generated.
    And now I'm wondering if I should bother publishing it to the Vaadin Directory. Two other comparable add-ons I found didn't seem to get much attention and are outdated to varying degrees without anyone complaining.

  12. svenruppert commented on Nov 28, 2024

    @svenruppert

    ohh, an old topic. :-) Well, the best way would be inside the framework. It could be generated to be in sync with every version. But it is not on the prio list.. means.. it will not happen. If you created the new project, let me know. I would have a view on it to try it out. Cheers Sven

  13. mstahv commented on Nov 28, 2024

    @mstahv
    Member

    I have no idea how much of Viritin usage is for the fluent API it provides for the core components. Personally at least I have been using those quite much, kind of as a more readable/flexible constrcutor.

    Lately, my thinking about this topic has evolved a bit though. I nowadays favor separate classes whenever I see n+1 (where n is currently somewhere between 0-2) setters or fluent configurations on an object. For example this snippet from above (derived to Viritin's API):

        private final VerticalLayout layout = new VVerticalLayout()
                .withDefaultHorizontalComponentAlignment(Alignment.START)
                .withSizeUndefined()
                .withComponent(new VHorizontalLayout(username, password))
                .withComponent(rememberMe)
                .withComponent(new VHorizontalLayout(btnLogin , btnCancel));

    would become something like

        private class LoginLayout extends VerticalLayout {
            public LoginLayout() {
                setDefaultHorizontalComponentAlignment(Alignment.START);
                setSizeUndefined();
                add(
                        new HorizontalLayout(username, password),
                        rememberMe,
                        new HorizontalLayout(btnLogin, btnCancel));
            }
        }
    
        private final LoginLayout layout = new LoginLayout();

    Even as anonymous class, it gives some structure, but as a named class it can be easily moved to the end of the class (and leave the more important details to be more readably in the beginning of the class. I actaully have an article in the publishing pipeline with a title "Don't be afraid of classes" :-)

    But yeah, still, I'd appreciate the feature. It would be neat in certain cases, better than polluting constrcutors) and it would for sure be (ab)used a lot.

  14. Frettman commented on Dec 1, 2024

    @Frettman

    I went with the fluent wrapper route since that allows those (short lived) wrappers to provide all the conveniences you can come up with, while keeping the component API clean and concise. And unlike fluent subclasses, it also allows you to (re)configure any existing components, like those mapped from a template with @id.

    An example with some layouting could look like this:

    VerticalLayout content = verticalLayout().sizeFull().padding().spacing().get();
    
    button().icon(VaadinIcon.REFRESH).text("Refresh")
      .onClick(event -> {
        ...
      })
      .addTo(content, l -> l.alignEnd());
    
    Grid<MyItem> grid = grid(MyItem.class).addTo(content, l -> l.sizeFull()).get();
    grid.addColumn(...

    The most interesting part here is probably the .addTo(...) which condenses something like

    layout.add(button);
    layout.setAlignSelf(Alignment.END, button);
    layout.setFlexGrow(2, button);

    to usually a one-liner like

    .addTo(layout, l -> l.alignEnd().grow(2));

    That way you won't even need shortcuts like new HorizontalLayout(username, password), because you can create your component hierarchy top down (you can create the parent layout before you create the child components which just feels more natural to me) and there's no sudden jump in complexity or fiddlyness when you need to do more than just add the component.

    Anyway, I created the release now (via Maven Central), so feel free to take a look: https://gitlab.com/codecamp-de/vaadin-flow-ui-toolkit#fluent-api-for-vaadin-flow
    Any feedback is of course welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions