Divider.swift 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /// A divider that expands along the minor axis of the containing stack layout.
  2. ///
  3. /// If not contained within a stack, this view expands horizontally.
  4. ///
  5. /// In dark mode it's white with 10% opacity, and in light mode it's black with
  6. /// 10% opacity.
  7. public struct Divider: View, Sendable {
  8. @Environment(\.colorScheme) var colorScheme
  9. @Environment(\.layoutOrientation) var layoutOrientation
  10. let requestedColor: Color?
  11. /// Creates a divider. Uses the provided color, or adapts to the current
  12. /// color scheme if nil.
  13. public init(_ color: Color? = nil) {
  14. self.requestedColor = color
  15. }
  16. var color: Color {
  17. requestedColor ?? Color.adaptive(light: .black, dark: .white)
  18. }
  19. public var body: some View {
  20. color
  21. .opacity(0.1)
  22. .frame(
  23. width: layoutOrientation == .horizontal ? 1 : nil,
  24. height: layoutOrientation == .vertical ? 1 : nil
  25. )
  26. }
  27. public var _asMenuItems: [MenuItem] {
  28. [.separator(self)]
  29. }
  30. }