Skip to content

Instantly share code, notes, and snippets.

@choonkending
Created September 21, 2016 23:13
Show Gist options
  • Save choonkending/705827e0523adbdcef46c2d7ea13a6d3 to your computer and use it in GitHub Desktop.
Save choonkending/705827e0523adbdcef46c2d7ea13a6d3 to your computer and use it in GitHub Desktop.
/* Modal */
<div className="modal">
<CloseButton />
</div>
/* CloseButton */
<button className="close-button">Close</button>
@choonkending
Copy link
Author

choonkending commented Sep 21, 2016

Pretty bad for re-usability because we shouldn't assume its position to always be the same whatever its parent context.

.close-button {
  color: red;
  position: absolute;
  top: 10px;
}

@choonkending
Copy link
Author

Better but

  • Can't edit Modal s styles to use another button without having to refactor modal.css
  • nested declaration makes debugging harder with unpredictable specificity
/* close-button.css */
.close-button {
   red;
}

/* modal.css */
.modal .close-button {
   position: absolute;
   top: 10px;
}

@choonkending
Copy link
Author

Better:

  • close button is an element of modal

Still not good:

  • Extra DOM nesting
/* modal.css */
.modal__close-button {
   position: absolute;
   top: 10px;
}
/* Modal.js */
<div className="modal">
   <div className="modal__close-button">
      <CloseButton />
   </div>
</div>

@choonkending
Copy link
Author

choonkending commented Sep 22, 2016

/* Modal.js */
<div className="modal">  
  <CloseButton className="modal__close-button"/>  
</div>  

/* CloseButton.jsx */  
<button className=`close-button ${this.props.className || ''}`>Close</button>  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment