Display a Text() with a foreground color based on a condition I decided to write a simple tab bar for macOS using Swift.
tabs.swift
import SwiftUI
import Foundation
import Combine

class ViewRouter: ObservableObject {
  @Published var currentView = &quot;home&quot;
}

struct Tabs: View {
  
  @ObservedObject var viewRouter = ViewRouter()
  
  var body: some View {
    GeometryReader { geometry in
      VStack {
        if self.viewRouter.currentView == &quot;home&quot; {
          Text(&quot;Home&quot;)
        } else if self.viewRouter.currentView == &quot;settings&quot; {
          Text(&quot;Settings&quot;)
        }
        Spacer()
        
        HStack {
          { () -&gt; Text in
            if self.viewRouter.currentView == &quot;home&quot; {
              return Text(&quot;Home&quot;).foregroundColor(Color.blue)
            } else {
              return Text(&quot;Home&quot;)
            }
          }()
          .onTapGesture {
            self.viewRouter.currentView = &quot;home&quot;
          }
          
          { () -&gt; Text in
            if self.viewRouter.currentView == &quot;settings&quot; {
              return Text(&quot;Settings&quot;).foregroundColor(Color.blue)
            } else {
              return Text(&quot;Settings&quot;)
            }
          }()
          .onTapGesture {
            self.viewRouter.currentView = &quot;settings&quot;
          }
        }
      }.edgesIgnoringSafeArea(.bottom).padding(20)
    }
  }
}

In ContentView.swift, I just display this using Tabs() in the ContentView struct.
In the HStack, I use two anonymous functions to give the text in the tab bar a blue color when that item is selected:
{ () -&gt; Text in
  if self.viewRouter.currentView == &quot;home&quot; {
    return Text(&quot;Home&quot;).foregroundColor(Color.blue)
  } else {
    return Text(&quot;Home&quot;)
  }
}()
  .onTapGesture {
    self.viewRouter.currentView = &quot;home&quot;
  }

I also do this for the second item in the tab bar.
Is this the best (fastest and most concise) way to do this, or is there a better way?
There is a shorter and faster way, using ternary operators.
You could use
Text(&quot;Home&quot;).foregroundColor(self.viewRouter.currentView == &quot;home&quot; ? Color.blue: Color.black)

instead  of
{ () -&gt; Text in
  if self.viewRouter.currentView == &quot;home&quot; {
    return Text(&quot;Home&quot;).foregroundColor(Color.blue)
  } else {
    return Text(&quot;Home&quot;)
  }
}()

However, with the advent of macOS Mojave 10.14 two years ago, users had an option to enable dark mode. So, unless you've explicitly disabled dark mode in your app, Color.black will blend in with the rest of your app bar. To prevent this, check for dark mode with UserDefaults.standard.object(forKey: &quot;AppleInterfaceStyle&quot;). If the user is using light mode, UserDefaults.standard.object(forKey: &quot;AppleInterfaceStyle&quot;) will be nil, and if it's dark mode, it will be &quot;Dark&quot;
Text(&quot;Home&quot;).foregroundColor(self.viewRouter.currentView == &quot;home&quot; ? Color.blue: UserDefaults.standard.object(forKey: &quot;AppleInterfaceStyle&quot;) == nil ? Color.black: Color.white)


As @BenLeggiero has suggested, this is a much shorter way to do the above:
Text(&quot;Home&quot;).foregroundColor(viewRouter.currentView == &quot;home&quot; ? .blue : .primary)