Skip to content

Add utility functions for enhanced attrs and styles functionality#167

Open
Snehadas2005 wants to merge 4 commits into
chasefleming:mainfrom
Snehadas2005:temp
Open

Add utility functions for enhanced attrs and styles functionality#167
Snehadas2005 wants to merge 4 commits into
chasefleming:mainfrom
Snehadas2005:temp

Conversation

@Snehadas2005

Copy link
Copy Markdown

Add new utility functions to improve developer experience when working with data attributes and CSS functions.

Proposed Changes

attrs package

  • MergeDataAttrs: Create multiple data attributes from a map in one go
    attrs.MergeDataAttrs(map[string]string{"id": "123", "role": "admin"})
    // Returns: Props{"data-id": "123", "data-role": "admin"}

styles package

  • Calc: CSS calc() function wrapper
  • Min: CSS min() function wrapper
  • Max: CSS max() function wrapper
  • Clamp: CSS clamp() function for responsive values
  • IsValidHexColor: Validate hex color format (#RGB or #RRGGBB)
  • IsValidRGBColor: Validate RGB color format

Benefits

  • Simplifies working with multiple data attributes
  • Type-safe CSS math functions for responsive design
  • Built-in color validation for error prevention
  • Maintains consistency with existing API patterns

Testing

  • Comprehensive unit tests for all new functions
  • Edge case coverage for validators
  • 100% test coverage for new code

Documentation

  • Function documentation with examples
  • Updated README files for both packages

I had worked on this contribution under the label of Hacktoberfest 2025.

@chasefleming chasefleming left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the additions! I think the merge method and validity checkers might not fit the scope of the library. Can we remove those and just keep the CSS helpers? Those are very useful and work well with this.

Comment thread attrs/utils.go Outdated
}

// MergeDataAttrs creates a Props map with multiple data attributes.
func MergeDataAttrs(dataAttrs map[string]string) Props {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is necessary.

The current API already handles this use case cleanly with the existing DataAttr() helper and Props type.

For creating multiple data attributes:

  userAttrs := attrs.Props{
      attrs.DataAttr("user-id"):   "123",
      attrs.DataAttr("user-name"): "john",
      attrs.DataAttr("role"):      "admin",
  }

If you need to combine data attributes with other attributes:

  buttonAttrs := attrs.Merge(
      attrs.Props{
          attrs.Class: "user-button",
          attrs.ID:    "user-123",
      },
      attrs.Props{
          attrs.DataAttr("user-id"): "123",
          attrs.DataAttr("role"):    "admin",
      },
  )

Or combining multiple attribute sets:

  finalAttrs := attrs.Merge(
      baseAttrs,
      attrs.Props{
          attrs.DataAttr("user-id"):   "123",
          attrs.DataAttr("user-name"): "john",
      },
  )

Comment thread styles/utils.go
}

// Calc returns a CSS calc() function string.
func Calc(expression string) string {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These CSS helpers are great. These would fit nicely in the library.

Comment thread styles/utils.go
}

// IsValidHexColor validates if a string is a valid hex color. Supports 3 or 6 digit hex codes.
func IsValidHexColor(color string) bool {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I don't think this fits the scope of the library.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will make changes accordingly. I will just keep the CSS part, and the rest of them will be restored to the initial one.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had made changes, kindly check

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see this in

Comment thread elem-demo/main.go Outdated
@@ -0,0 +1,38 @@
package main

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this too please?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly check once again.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants