title: SwiftUI ButtonStyle - how to check if button is disabled or enabled?
body: <p>To style a button in SwiftUI, according to my understanding, you extend <code>ButtonStyle</code> and implement <code>func makeBody(configuration: Self.Configuration) -&gt; some View</code> within which you start applying modifications to the <code>configuration.label</code> which is a reference to the <code>Button</code> view.</p>
  
  <p>Now, besides the <code>label</code> field, <code>ButtonStyle.Configuration</code> has a boolean field for <code>isPressed</code>, but that's seems to be all.</p>
  
  <p>How do I check if the button is enabled or disabled?</p>
  
  <p>For example, I want to draw a border around the button, and I want the border to be blue if the button is enabled and gray if disabled.</p>
  
  <p>My first guess is something like this:</p>
  
  <pre><code>    func makeBody(configuration: Self.Configuration) -&gt; some View {
          configuration.label
              .overlay(
                  RoundedRectangle(cornerRadius: 4).stroke(configuration.isEnabled ? Color.blue : Color.gray, lineWidth: 1).padding(8)
              )
      }
  </code></pre>
  
  <p>But there's no <code>isEnabled</code> field.</p>
  
  <p>The Apple-supplied <code>PlainButtonStyle</code> obviously has access to this information, as the doc comment in the header file that declares it says 'The style may apply a visual effect to indicate the pressed, focused, or enabled state of the button.'</p>
  
  <pre><code>/// A `Button` style that does not style or decorate its content while idle.
  ///
  /// The style may apply a visual effect to indicate the pressed, focused,
  /// or enabled state of the button.
  @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  public struct PlainButtonStyle : PrimitiveButtonStyle {
  .....
  </code></pre>
  
  <p>Is there a way to access this information?</p>
  
  <h2>EDIT:</h2>
  
  <p>Before you suggest closing this question, please read this:</p>
  
  <p>There's a similar question asked a while ago. That question does not specify any context.</p>
  
  <p>Here I have a specific context: creating a generic reusable button style.</p>
  
  <p>The answer provided there does not suffice. I can't expect people to pass the disabled state to the style constructor. That's too much duplication of effort.</p>
  
  <p>Can you imagine if people have to write code this way:</p>
  
  <p><code>Button() { .... }.disabled(someExprssion).buttonStyle(MyCustomStyle(disabled: someExpression))</code></p>
  
  <p>Clearly that's not desireable.</p>
  
  <p>Also clearly the Apple supplied style has access to the information without requiring people to pass the disable state again to the style.</p>
  
  <p>If you close the question, you will forever prevent anyone from providing a useful answer to this question on StackOverflow.</p>
  
  <p>Please reconsider before you propose closing.</p>
  
  <h2>EDIT2:</h2>
  
  <p>I have a guess that if you can get the <code>EnvironmentValues.isEnabled</code> of the <code>Button</code> view then it might be the right value.</p>
  
  <p>So another way to ask the question is: is it possible in SwiftUI to get the environment of a view that you didn't define yourself?</p>