Skip to content
Closed
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
94 changes: 94 additions & 0 deletions text/0000-component-boolean-arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
- Start Date: 11/30/2018
- RFC PR: (leave this empty)
- Ember Issue: (leave this empty)

# Component boolean Arguments

## Summary

This RFC proposes to add a boolean argument to components, that aligns with boolean attribute of regular DOM elements.

## Motivation

Currently to pass a boolean property to a component a developer has to explicitly pass `true` as an argument.
e.g.
```hbs
<Button @secondary={{true}} />
```
This does allow the value to be a variable e.g.

```hbs
<Dialog @visible={{this.visible}} />
```

### Hacky "solution"
If the value is not a variable `{{}}` can actually be ommited as a `true` string is truthy leading to somewhat easier to remember syntax of
```hbs
<MyHackyBoolArgument @bool=true />
```
Since this is hacky, one have to remember that
```hbs
<MyHackyBoolArgument @bool=false />
```
would not work as `@bool=false` would pass a `false` as string that is truthy in JavaScript.

For simple usecases it is verbose to write and does not align with HTML attributes like `disabled` of which value can be ommited as desribed in [HTML Spec](https://www.w3.org/TR/html5/infrastructure.html#sec-boolean-attributes)

## Detailed design

This RFC proposes that if an argument is passed without any value it should have a value `true` set inside of a component.

e.g.
```hbs
<Button @secondary />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possible drawback or unexpected consequence of this is that NOT passing @secondary doesn't make the property false but undefined. While it makes sense, if feels weird that instead of the usual true/false dichotomy we're dealing with true/undefined

```
inside the Button component `@secondary` should equal to `true`.
e.g.
```hbs
<button class={{if @secondary "is-secondary"}}>
Button
</button>
```

should render as
```html
<button class="is-secondary">
Button
</button>
```

### Align with attributes
This would align with how attributes behave currently.
Given an Input component:
```hbs
<input ...attributes>
```

if it is invoked like this:
```hbs
<Input disabled />
```

it will render as
```html
<input disabled>
```


## How we teach this

This should be included in guides and also should be easier to teach as it is inline with HTML spec for attributes.

## Drawbacks

Can't think of any at the moment.

## Alternatives

- In both React and Vue if a prop is passed with no value it is truthy inside of a component.
- https://vuejs.org/v2/guide/components-props.html#Passing-a-Boolean
- https://reactjs.org/docs/jsx-in-depth.html#props-default-to-true

## Unresolved questions

None at the moment.