palexdev / MaterialFX

A library of material components for JavaFX
GNU Lesser General Public License v3.0
1.2k stars 122 forks source link

Feature Enhancement Proposal: `CSSFragment` API Improvements #388

Open infinite-dev22 opened 2 hours ago

infinite-dev22 commented 2 hours ago

I’d like to propose some enhancements to the CSSFragment API, which could further streamline development workflows and align the API for convenience and clarity.

Proposed Enhancements for CSSFragment

After working with the current API, I believe the following improvements could streamline its functionality and enhance the developer experience:

  1. Support for Enum-based Selectors Enhancing the .addSelector() method to accept predefined, enum-like values representing standard CSS selectors would improve readability and reduce errors associated with raw string usage. This approach would be particularly beneficial when working with complex or multi-selector styles.

  2. Direct Numeric Input for Size-related Properties Allowing direct numeric input for properties such as padding, margin, width, angles, etc. would enhance the intuitiveness of the API. By accepting int or double values, developers could eliminate repetitive string conversions, leading to a more streamlined and error-resistant workflow for numeric style properties.

  3. Implicit Selector Closure in .build() Integrating .closeSelector() functionality within .build() would simplify the usage of CSSFragment by removing the need for explicit selector closure calls. This modification would yield a cleaner API that supports a more natural chaining style, particularly beneficial when applying multiple styles in sequence.

Minimal Reproducible Example (MRE)

To illustrate, here’s an example using the proposed improvements:

CSSFragment(sp).addSelector(".css-class-selector")
               .padding(20)
               .background("transparent")
               .build();

This design streamlines the current approach, which requires multiple calls as follows:

CSSFragment.Builder.build()
                   .addSelector(".css-class-selector")
                   .padding("20px")
                   .background("transparent")
                   .closeSelector()
                   .applyOn(sp);

Thank you for considering these enhancements.

palexdev commented 2 hours ago

Thank you very much, will look into it asap