<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: nakajmg</title>
    <description>The latest articles on DEV Community by nakajmg (@nakajmg).</description>
    <link>https://dev.to/nakajmg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F41543%2Fbf03c1fb-da09-409b-b6c4-726d1fc34471.png</url>
      <title>DEV Community: nakajmg</title>
      <link>https://dev.to/nakajmg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nakajmg"/>
    <language>en</language>
    <item>
      <title>CSS naming convention in Vue component</title>
      <dc:creator>nakajmg</dc:creator>
      <pubDate>Fri, 30 Mar 2018 02:38:42 +0000</pubDate>
      <link>https://dev.to/nakajmg/css-naming-convention-in-vue-component-46o5</link>
      <guid>https://dev.to/nakajmg/css-naming-convention-in-vue-component-46o5</guid>
      <description>&lt;p&gt;For a long time I considered CSS naming convention in Vue component.&lt;/p&gt;

&lt;p&gt;This naming convention is one of my answer. It's less complexity and easy to handling.&lt;/p&gt;

&lt;p&gt;I thought about CSS naming conventions tailored to the Vue component&lt;br&gt;
Previously, SFC (Single File Component) used BEM nomenclature, but I felt redundant in writing components. Therefore, based on ECSS's idea, I calmed down to customize it according to SFC.&lt;/p&gt;

&lt;p&gt;It is called &lt;em&gt;SFCSS&lt;/em&gt; that means CSS for SFC.&lt;/p&gt;
&lt;h2&gt;
  
  
  Naming convention is necessary even using &lt;code&gt;scoped&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;First of all as a prerequisite, a naming convention is necessary even if &lt;code&gt;&amp;lt;style scoped&amp;gt;&lt;/code&gt; does not leak out the style of the component. The reason for class name written appropriately according to &lt;code&gt;scoped&lt;/code&gt; will surely be paid later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vuejs.org/v2/style-guide/index.html#Component-style-scoping-essential"&gt;Official style guide#Component style scoping&lt;/a&gt; In order not to apply it, it is necessary to add a component specific prefix and so on.&lt;/p&gt;

&lt;p&gt;However, development at SFC is different from ordinary web site in nature, so I think that the prefix is ​​not necessarily the correct answer.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example of naming in ECSS
&lt;/h2&gt;

&lt;p&gt;As an example, ECSS naming is done with the following pattern.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.namespace-Component_ChildNode-variant&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;namespace&lt;/code&gt; gives prefix as to where the component belongs etc &lt;code&gt;-variant&lt;/code&gt; is a modifier in BEM. Where is &lt;code&gt;Component&lt;/code&gt; Block and&lt;code&gt;ChildNode&lt;/code&gt; Element?&lt;/p&gt;

&lt;p&gt;We adapted this naming convention to SFC as follows.&lt;/p&gt;

&lt;h2&gt;
  
  
  SFCSS
&lt;/h2&gt;

&lt;p&gt;In SFCSS, naming is done with the following pattern.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.ComponentName&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.ComponentName_ChildNode&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;._variant&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only these three patterns are used.&lt;/p&gt;

&lt;h2&gt;
  
  
  ComponentName
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ComponentName&lt;/code&gt; uses the same name as the&lt;code&gt;name&lt;/code&gt; of the vue component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"MyComponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyComponent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style &lt;/span&gt;&lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"scss"&lt;/span&gt; &lt;span class="na"&gt;scoped&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;.MyComponent&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By matching the root class name with the &lt;code&gt;name&lt;/code&gt; of the vue component, it is easier to identify the component from the DOM node.&lt;/p&gt;

&lt;p&gt;If you can use vue-devtools, this idea is not necessary, but it will be useful in production environments and verification in browsers without vue-devtools.&lt;/p&gt;

&lt;h2&gt;
  
  
  ChildNode
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ChildNode&lt;/code&gt; is an element other than the root element of the component to which you want to grant the class.&lt;/p&gt;

&lt;p&gt;For example, set &lt;code&gt;ChildNode&lt;/code&gt; with the following granularity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"MyComponent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"MyComponent_Heading"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      heading
    &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"MyComponent_List"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"MyComponent_ListItem"&lt;/span&gt;
        &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"item in list"&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        {{item.label}}
      &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  bad pattern
&lt;/h4&gt;

&lt;p&gt;In principle, the following &lt;code&gt;ChildNode&lt;/code&gt; consecutive class names shall be &lt;strong&gt;unacceptable&lt;/strong&gt; 🙅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.MyComponent_List_Item&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;bad&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  variant
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;variant&lt;/code&gt; is a class to use for elements whose style you want to change depending on conditions. ECSS contains a full selector such as &lt;code&gt;ComponentName_ChildNode&lt;/code&gt; as a class name, but since you can use&lt;code&gt;scoped&lt;/code&gt;, define it as a single class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ComponentName&lt;/code&gt; and &lt;code&gt;ChildNode&lt;/code&gt; are Pascal cases, whereas &lt;code&gt;variant&lt;/code&gt; is written in a camel case and begins with a&lt;code&gt;_&lt;/code&gt; at the beginning of the class name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;._variantClassName&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;variant&lt;/code&gt; has many uses to detach it with&lt;code&gt;: class&lt;/code&gt;, it is convenient to construct it with a character string that does not require quotes.&lt;/p&gt;

&lt;h3&gt;
  
  
  namespace is unnecessary
&lt;/h3&gt;

&lt;p&gt;We considered &lt;code&gt;namespace&lt;/code&gt; unnecessary in the vue component. The &lt;code&gt;namespace&lt;/code&gt; of ECSS indicates the area to which the component belongs etc, but there is no need to limit the place to be used in the vue component.&lt;/p&gt;

&lt;p&gt;You will not need to avoid name conflicts due to prefixes as vue components will have unique names.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limit the root class to one
&lt;/h2&gt;

&lt;p&gt;Within &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; of &lt;code&gt;.vue&lt;/code&gt;, limit the root class (&lt;code&gt;ComponentName&lt;/code&gt;) to one. Only styles are described with components that are one-to-one.&lt;/p&gt;

&lt;p&gt;If you want to write more than one root class, it may be a sign to separate into different components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use &lt;code&gt;lang="scss"&lt;/code&gt; to omit description with &lt;code&gt;&amp;amp;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For example, in &lt;code&gt;template&lt;/code&gt; exemplified by&lt;code&gt;ChildNode&lt;/code&gt; above, write &lt;code&gt;style&lt;/code&gt; as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"MyComponent"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"MyComponent_Heading"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;heading&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"MyComponent_List"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"MyComponent_ListItem"&lt;/span&gt;
        &lt;span class="nt"&gt;v-for&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"item in list"&lt;/span&gt;
        &lt;span class="nd"&gt;:class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"{_selectedItem: isSelectedItem(item)}"&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="nt"&gt;item&lt;/span&gt;&lt;span class="nc"&gt;.label&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;style&lt;/span&gt; &lt;span class="nt"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"scss"&lt;/span&gt; &lt;span class="nt"&gt;scoped&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;.MyComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;_Heading&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;_List&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nt"&gt;_ListItem&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="nc"&gt;._selectedItem&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;style&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although the amount of descriptions of class names in &lt;code&gt;template&lt;/code&gt; does not decrease, we thought that the overall&lt;code&gt;style&lt;/code&gt; looks better as compared to writing all in full. This &lt;code&gt;&amp;amp; _&lt;/code&gt; omission is also useful for partitioning components.&lt;/p&gt;

&lt;h2&gt;
  
  
  in conclusion
&lt;/h2&gt;

&lt;p&gt;When I tried this SFCSS in the actual case, I did not suffer from the class name of the component. I think that development is proceeding with a well-balanced state that it is not properly named.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post is translation by Google Translate&lt;/em&gt;. &lt;a href="https://qiita.com/nakajmg/items/683395c20a3afbdb2d99"&gt;Original post in Japanese&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vue</category>
      <category>css</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
