Table of Contents for
Mastering PostCSS for Web Design

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering PostCSS for Web Design by Alex Libby Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Mastering PostCSS for Web Design
  4. Mastering PostCSS for Web Design
  5. Credits
  6. About the Author
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Introducing PostCSS
  16. Introducing PostCSS
  17. Setting up a development environment
  18. Creating a simple example using PostCSS
  19. Linting code using plugins
  20. Exploring how PostCSS works
  21. Summary
  22. 2. Creating Variables and Mixins
  23. Creating a hover effect example
  24. Transitioning to using PostCSS
  25. Adding variable support to PostCSS
  26. Updating our hover effect demo
  27. Setting the order of plugins
  28. Creating mixins with PostCSS
  29. Looping content with PostCSS
  30. Summary
  31. 3. Nesting Rules
  32. Navigating through pages
  33. Transitioning to using PostCSS plugins
  34. Exploring the pitfalls of nesting
  35. Making the switch to BEM
  36. Exploring our changes in more detail
  37. Summary
  38. 4. Building Media Queries
  39. Exploring custom media queries in PostCSS
  40. Making images responsive
  41. Adding responsive text support
  42. Optimizing media queries
  43. Retrofitting support for older browsers
  44. Moving away from responsive design
  45. Taking things further with CSS4
  46. Summary
  47. 5. Managing Colors, Images, and Fonts
  48. Managing fonts with PostCSS
  49. Creating image sprites
  50. Working with SVG in PostCSS
  51. Adding support for WebP images
  52. Manipulating colors and color palettes
  53. Creating color functions with PostCSS
  54. Summary
  55. 6. Creating Grids
  56. Creating an example with Bourbon Neat
  57. Exploring the grid plugins in PostCSS
  58. Transitioning to using PostCSS-Neat
  59. Creating a site using Neat and PostCSS
  60. Adding responsive capabilities
  61. Summary
  62. 7. Animating Elements
  63. Moving away from jQuery
  64. Making use of pre-built libraries
  65. Switching to using SASS
  66. Making the switch to PostCSS
  67. Exploring plugin options within PostCSS
  68. Updating code to use PostCSS
  69. Creating a demo in PostCSS
  70. Optimizing our animations
  71. Using our own animation plugin
  72. Summary
  73. 8. Creating PostCSS Plugins
  74. Dissecting the architecture of a standard plugin
  75. Creating an transition plugin
  76. Building a custom font plugin
  77. Simplifying the development process
  78. Guidelines for plugin building
  79. Making the plugin available for use
  80. Summary
  81. 9. Working with Shortcuts, Fallbacks, and Packs
  82. Exploring plugin packs for PostCSS
  83. Adding shortcuts with Rucksack
  84. Linting and optimizing your code
  85. Providing fallback support
  86. Summary
  87. 10. Building a Custom Processor
  88. Exploring our processor
  89. Dissecting issues with our processor
  90. Optimizing the output
  91. Adding reload capabilities
  92. Extending our processor further
  93. Testing the final pre-processor
  94. Getting started with some hints and tips
  95. Introducing the CSStyle library
  96. Summary
  97. 11. Manipulating Custom Syntaxes
  98. Preparing our environment
  99. Implementing custom syntax plugins
  100. Parsing CSS
  101. Formatting the output with the API
  102. Highlighting our syntax code
  103. Summary
  104. 12. Mixing Preprocessors
  105. Exploring the conversion process
  106. Introducing the Pleeease library
  107. Compiling with other preprocessors
  108. Using the PreCSS library
  109. Converting a WordPress installation
  110. Setting up our environment
  111. Considering the conversion process
  112. Making changes to our code
  113. Compiling and testing the changes
  114. Summary
  115. 13. Troubleshooting PostCSS Issues
  116. Exploring some common issues
  117. Getting help from others
  118. Summary
  119. 14. Preparing for the Future
  120. Converting CSS4 styles for use
  121. Supporting future syntax with cssnext
  122. Creating plugins to provide extra CSS4 support
  123. Summary
  124. Index

Using our own animation plugin

Throughout the course of this chapter, we've used the small number of animation-based plugins that are available for PostCSS, and demonstrated some of the effects possible. This is all well and good, but one can't help but feel that this is a little limiting—and can we do something about it?

Absolutely, the beauty of PostCSS is that if there is a need for a plugin, then we can create something to fill that gap. A perfect example of this is the lack of CSS-based animation plugins available; at present, all we have is postcss-animations, which inserts animations from the Animate.css style sheet created by Dan Eden. I've used this as a basis for a new plugin—we'll use the same framework, but convert it to use the Magic set of animations, available from http://www.minimamente.com/example/magic_animations/.

We will cover the construction of plugins in more detail in Chapter 8, Creating PostCSS Plugins. Let's make a start:

  1. From the code download that accompanies this book, go ahead and extract a copy of the T41 folder, and save the contents to the root of our project area.
  2. Copy the postcss-animation and postcss-animation-data folders to the node_modules folder within our project area.
  3. Copy the gulpfile.js and package.json files to the root of our project area—if any are already present, replace them (or take copies for safekeeping).
  4. Crack open your text editor and add the following code, saving it as style.css in the src folder of our project area:
    .foo {
      animation-name: openDownLeft;
    }
  5. Fire up a Node.js folder, then change the working folder to our project area.
  6. At the prompt, enter gulp, then press Enter—PostCSS will go away and compile the code; if all is well, we should see the @keyframes code added to our compiled style sheet (in the dest folder), as shown in this screenshot:
    Using our own animation plugin

Although our example only shows the single style, this doesn't matter—any style sheet that uses animation-name can be used, provided the animation-name value used matches one in the postcss-animation-data plugin. There are a few key points, though, that we should cover, so let's take a moment to explore these in more detail.

Exploring the plugin in more detail

Our new plugin is a perfect example of how we can adapt an existing framework to use different values—there are a few key points we should note when using this plugin:

  • A key point to consider when constructing any plugin: don't worry about adding vendor prefixes. These should be added as part of the compilation stage when the plugin is used within your projects; this will take care of any vendor prefixes that are required.
  • At present, the plugin only lists two animation types from the Magic Animations library as examples—the full list of original animations is available from the Magic Animations GitHub repository at https://github.com/miniMAC/magic/blob/master/magic.css. We can easily add in any that we need, using the format of "<name of animation>" : "<keyframe to use>", as shown in this screenshot:
    Exploring the plugin in more detail

    As an experiment, how about trying to convert the animations from the Motion UI library at http://zurb.com/playground/motion-ui, for example? Or we can try the animations for AngularJS at http://www.justinaguilar.com/animations/# - it's entirely up to you!

  • Alternatively, it's worth applying the same principles to the postcss-easings plugin available from https://github.com/postcss/postcss-easings; this has some well-known easings built in, but they can easily be replaced. A great tool for this purpose is the site at http://www.cubic-bezier.com. For example, if we take the easeInExpo easing, we create a Bezier curve that looks something like this:
    Exploring the plugin in more detail

Note

This translates to a value of cubic-bezier(.95,.05,.79,.35), which we can use in our code. It's worth noting that some sites will show this easing as (0.05, 0.795, 0.035)http://cubic-bezier.com/ only shows values to two decimal places.

There are plenty of ways we can extend, modify, or create new plugins—the key to any should be that they are kept simple, limited to one task only, and that where possible, you should use the PostCSS plugin boilerplate as the basis for creating the plugins. The plugin we used in this exercise was created manually—this isn't an issue if you are creating it for your own needs, and don't intend to publish the plugin. In the next chapter, we will explore how easy it is to create something using the boilerplate code—it avoids a lot of issues at a later date!