Skip to content

Commit

Permalink
Update event modifiers to use HTMLModifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
JPToroDev committed Jan 18, 2025
1 parent 3e0fbcc commit 592fc7b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 35 deletions.
42 changes: 42 additions & 0 deletions Sources/Ignite/Actions/Event Modifiers/ClickModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// ClickModifier.swift
// Ignite
// https://www.github.com/twostraws/Ignite
// See LICENSE for license information.
//

/// A modifier that adds a click event handler to an HTML element.
struct ClickModifier: HTMLModifier {
let actions: [Action]

func body(content: some HTML) -> any HTML {
content.addEvent(name: "onclick", actions: actions)
}
}

public extension HTML {
/// Adds an "onclick" JavaScript event to this element.
/// - Parameter actions: A closure that returns the actions to execute when clicked.
/// - Returns: A modified HTML element with the click event handler attached.
func onClick(@ActionBuilder actions: () -> [Action]) -> some HTML {
modifier(ClickModifier(actions: actions()))
}
}

public extension InlineHTML {
/// Adds an "onclick" JavaScript event to this inline element.
/// - Parameter actions: A closure that returns the actions to execute when clicked.
/// - Returns: A modified inline HTML element with the click event handler attached.
func onClick(@ActionBuilder actions: () -> [Action]) -> some InlineHTML {
modifier(ClickModifier(actions: actions()))
}
}

public extension BlockHTML {
/// Adds an "onclick" JavaScript event to this block element.
/// - Parameter actions: A closure that returns the actions to execute when clicked.
/// - Returns: A modified block HTML element with the click event handler attached.
func onClick(@ActionBuilder actions: () -> [Action]) -> some BlockHTML {
modifier(ClickModifier(actions: actions()))
}
}
42 changes: 42 additions & 0 deletions Sources/Ignite/Actions/Event Modifiers/DoubleClickModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// DoubleClickModifier.swift
// Ignite
// https://www.github.com/twostraws/Ignite
// See LICENSE for license information.
//

/// A modifier that adds a double click event handler to an HTML element.
struct DoubleClickModifier: HTMLModifier {
let actions: [Action]

func body(content: some HTML) -> any HTML {
content.addEvent(name: "ondblclick", actions: actions)
}
}

public extension HTML {
/// Adds an "ondblclick" JavaScript event to this element.
/// - Parameter actions: A closure that returns the actions to execute when double-clicked.
/// - Returns: A modified HTML element with the double click event handler attached.
func onDoubleClick(@ActionBuilder actions: () -> [Action]) -> some HTML {
modifier(DoubleClickModifier(actions: actions()))
}
}

public extension InlineHTML {
/// Adds an "ondblclick" JavaScript event to this inline element.
/// - Parameter actions: A closure that returns the actions to execute when double-clicked.
/// - Returns: A modified inline HTML element with the double click event handler attached.
func onDoubleClick(@ActionBuilder actions: () -> [Action]) -> some InlineHTML {
modifier(DoubleClickModifier(actions: actions()))
}
}

public extension BlockHTML {
/// Adds an "ondblclick" JavaScript event to this block element.
/// - Parameter actions: A closure that returns the actions to execute when double-clicked.
/// - Returns: A modified block HTML element with the double click event handler attached.
func onDoubleClick(@ActionBuilder actions: () -> [Action]) -> some BlockHTML {
modifier(DoubleClickModifier(actions: actions()))
}
}
54 changes: 54 additions & 0 deletions Sources/Ignite/Actions/Event Modifiers/HoverModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// EventModifiers.swift
// Ignite
// https://www.github.com/twostraws/Ignite
// See LICENSE for license information.
//

/// A modifier that adds hover event handlers to an HTML element.
struct HoverModifier: HTMLModifier {
let hoverActions: [Action]
let unhoverActions: [Action]

func body(content: some HTML) -> any HTML {
content
.addEvent(name: "onmouseover", actions: hoverActions)
.addEvent(name: "onmouseout", actions: unhoverActions)
}
}

public extension HTML {
/// Adds "onmouseover" and "onmouseout" JavaScript events to this element.
/// - Parameter actions: A closure that takes a Boolean indicating hover state and returns actions to execute.
/// - Returns: A modified HTML element with the hover event handlers attached.
func onHover(@ActionBuilder actions: (_ isHovering: Bool) -> [Action]) -> some HTML {
modifier(HoverModifier(
hoverActions: actions(true),
unhoverActions: actions(false)
))
}
}

public extension InlineHTML {
/// Adds "onmouseover" and "onmouseout" JavaScript events to this inline element.
/// - Parameter actions: A closure that takes a Boolean indicating hover state and returns actions to execute.
/// - Returns: A modified inline HTML element with the hover event handlers attached.
func onHover(@ActionBuilder actions: (_ isHovering: Bool) -> [Action]) -> some InlineHTML {
modifier(HoverModifier(
hoverActions: actions(true),
unhoverActions: actions(false)
))
}
}

public extension BlockHTML {
/// Adds "onmouseover" and "onmouseout" JavaScript events to this block element.
/// - Parameter actions: A closure that takes a Boolean indicating hover state and returns actions to execute.
/// - Returns: A modified block HTML element with the hover event handlers attached.
func onHover(@ActionBuilder actions: (_ isHovering: Bool) -> [Action]) -> some BlockHTML {
modifier(HoverModifier(
hoverActions: actions(true),
unhoverActions: actions(false)
))
}
}
35 changes: 0 additions & 35 deletions Sources/Ignite/Actions/EventModifiers.swift

This file was deleted.

0 comments on commit 592fc7b

Please sign in to comment.