Skip to content

Commit

Permalink
Merge pull request #2 from quora/main
Browse files Browse the repository at this point in the history
Adding Collapsibility to ExpandableText
  • Loading branch information
n3d1117 committed Nov 4, 2023
2 parents 0a8b3fe + e1dc85b commit 4120372
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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

0 comments on commit 4120372

Please sign in to comment.