Skip to content

Commit

Permalink
feat(ui): Aspect Ratio component
Browse files Browse the repository at this point in the history
  • Loading branch information
rotmh committed Oct 9, 2024
1 parent 5aaee2b commit e2a8291
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 42 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
gen:
templ generate
./tailwindcss -i ./static/css/input.css -o ./static/css/output.css --minify
go generate ./...
2 changes: 1 addition & 1 deletion static/css/output.css

Large diffs are not rendered by default.

Binary file removed tailwindcss
Binary file not shown.
11 changes: 11 additions & 0 deletions ui/aspect-ratio/generated_optaliase.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions ui/aspect-ratio/root.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package aspectratio

import (
"fmt"
"github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
"github.com/rotemhoresh/shadcn-templ/ui"
)

type RootProps struct {
ratio float32
ui.CoreProps
}

type RootOption = ui.Option[*RootProps]

// note that `16/9` will result in `1`. you need to convert one of the numbers to float, e.g., `16.0/9.0`.
func WithRatio(r float32) RootOption {
return func(p *RootProps) {
p.ratio = r
}
}

//go:generate go run ../optalias_generator.go -type=RootProps

func Root(opts ...RootOption) templ.Component {
p := &RootProps{
ratio: 1 / 1,
CoreProps: ui.DefaultCoreProps,
}
for _, opt := range opts {
opt(p)
}
return root(p)
}

css rootStyle() {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}

css rootWrapperStyle(r float32) {
position: relative;
width: 100%;
padding-bottom: { fmt.Sprintf("%.2f%%", 100/r) };
}

templ root(props *RootProps) {
<div
class={ rootWrapperStyle(props.ratio) }
>
<div
class={ rootStyle(), twmerge.Merge(props.Class()) }
{ props.Attrs()... }
>
{ children... }
</div>
</div>
}
155 changes: 155 additions & 0 deletions ui/aspect-ratio/root_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions ui/button/root.templ
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,40 @@ func (s Size) Class() string {
}
}

type RootProps struct {
typ Type
variant Variant
size Size
ui.CoreProps
}

type RootOption = ui.Option[*RootProps]

func WithType(t Type) RootOption {
return func(p *RootProps) {
p.Type = t
p.typ = t
}
}

func WithVariant(v Variant) RootOption {
return func(p *RootProps) {
p.Variant = v
p.variant = v
}
}

func WithSize(s Size) RootOption {
return func(p *RootProps) {
p.Size = s
p.size = s
}
}

//go:generate go run ../optalias_generator.go -type=RootProps

type RootProps struct {
Type Type
Variant Variant
Size Size
ui.CoreProps
}

func Root(opts ...RootOption) templ.Component {
p := &RootProps{
Type: TypeButton,
Variant: VariantDefault,
Size: SizeDefault,
typ: TypeButton,
variant: VariantDefault,
size: SizeDefault,
CoreProps: ui.DefaultCoreProps,
}
for _, opt := range opts {
Expand All @@ -120,8 +120,8 @@ func Root(opts ...RootOption) templ.Component {

templ root(props *RootProps) {
<button
type={ props.Type.String() }
class={ twmerge.Merge(baseClass, props.Variant.Class(), props.Size.Class(), props.Class()) }
type={ props.typ.String() }
class={ twmerge.Merge(baseClass, props.variant.Class(), props.size.Class(), props.Class()) }
{ props.Attrs()... }
>
{ children... }
Expand Down
32 changes: 16 additions & 16 deletions ui/button/root_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e2a8291

Please sign in to comment.