Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Collapsibility to ExpandableText #2

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Example/ExpandableTextExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct ContentView: View {
.moreButtonColor(.red)
.expandAnimation(.easeInOut(duration: 2))
.trimMultipleNewlinesWhenTruncated(false)
.enableCollapse(true)
.border(.red)

ExpandableText("**Markdown** is _supported_")
Expand Down
11 changes: 11 additions & 0 deletions Sources/ExpandableText/ExpandableText+Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public extension ExpandableText {
return copy
}

/**
Enables collapsing behavior by tapping on the text body when the state is expanded.
- Parameter value: Whether or not to enable collapse functionality.
- Returns: A new `ExpandableText` instance with the specified collapse ability applied.
*/
func enableCollapse(_ value: Bool) -> Self {
var copy = self
copy.collapseEnabled = value
return copy
}

/**
Sets whether multiple consecutive newline characters should be trimmed when truncating the text in the `ExpandableText` instance.
- Parameter value: A boolean value indicating whether to trim multiple consecutive newline characters. Defaults to `true`
Expand Down
4 changes: 3 additions & 1 deletion Sources/ExpandableText/ExpandableText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public struct ExpandableText: View {
internal var moreButtonFont: Font?
internal var moreButtonColor: Color = .accentColor
internal var expandAnimation: Animation = .default
internal var collapseEnabled: Bool = false
internal var trimMultipleNewlinesWhenTruncated: Bool = true

/**
Expand Down Expand Up @@ -79,7 +80,8 @@ public struct ExpandableText: View {
)
.contentShape(Rectangle())
.onTapGesture {
if shouldShowMoreButton {
if (isExpanded && collapseEnabled) ||
shouldShowMoreButton {
withAnimation(expandAnimation) { isExpanded.toggle() }
}
}
Expand Down