DEV Community

Cover image for FSCSS copy() function
FSCSS tutorial
FSCSS tutorial

Posted on

FSCSS copy() function

The copy() function in FSCSS is a powerful tool for extracting and reusing portions of values, which is particularly useful when working with design tokens or lists of values. Let's break down the provided example to make it more detailed and understandable.
How copy(length, variable) Works
The copy() function takes two arguments:

  • length: This specifies how many characters (or the exact substring based on its length) you want to extract from the value.
    • A positive integer n means it extracts the first n characters from the beginning of the string.
    • A negative integer -n means it extracts the last n characters from the end of the string.
    • If the length is greater than the actual length of the string, it will likely copy the entire string.
  • variable: This is the name of the FSCSS variable where the extracted value will be stored. You can then reuse this variable throughout your FSCSS stylesheets. Practical Example Explained in Detail Let's dissect the provided FSCSS example:
body{
  /* primary-color = #4ff */
  background:#4ff000 copy(4, primary-color);
  color: $primary-color!;
}

/* use it in any sheets */
a{
  color: $primary-color!;
}

span:before{
  /* copy 'midnightblue' and store it as 'my-darkblue' */
  content: "blue or midnightblue copy(-14, my-darkblue)";
  border: 2px solid $my-darkblue!;
}
Enter fullscreen mode Exit fullscreen mode

Section 1: Extracting from background property

body{
  /* primary-color = #4ff */
  background:#4ff000 copy(4, primary-color);
  color: $primary-color!;
}
Enter fullscreen mode Exit fullscreen mode
  • background:#4ff000 copy(4, primary-color);
    • Here, the copy() function is applied to the value of the background property: #4ff000.
    • length is 4. This means FSCSS will extract the first 4 characters from #4ff000.
    • The extracted value will be #4ff.
    • variable is primary-color. So, the extracted value #4ff will be stored in a new FSCSS variable named $primary-color.
  • color: $primary-color!;
    • Now, the $primary-color variable, which holds the value #4ff, is used as the color for the body element.
    • The ! after $primary-color is a common FSCSS syntax for declaring a variable's importance or making it a final, non-overridable value within that scope (similar to !important in regular CSS, but for variable assignment). Section 2: Reusing the variable in other sheets
/selectors
/* use it in any sheets */
a{
  color: $primary-color!;
}
Enter fullscreen mode Exit fullscreen mode
  • This demonstrates the power of copy(). Once a value is extracted and stored in a variable (like $primary-color), it can be reused anywhere else in your FSCSS code.
  • Here, the a (anchor) elements will also have their color set to the value of $primary-color, which is #4ff. This promotes consistency and makes it easy to update a color across your design by changing it in just one place. Section 3: Extracting with a negative length and from a string literal
span:before{
  /* copy 'midnightblue' and store it as 'my-darkblue' */
  content: "blue or midnightblue copy(-14, my-darkblue)";
  border: 2px solid $my-darkblue!;
}
Enter fullscreen mode Exit fullscreen mode
  • content: "blue or midnightblue copy(-14, my-darkblue)";
    • Here, copy() is applied to a string literal within the content property: "blue or midnightblue".
    • length is -14. This tells FSCSS to extract the last 14 characters of the string.
    • Let's count the characters from the end of "blue or midnightblue":
      • e (1)
      • u (2)
      • l (3)
      • b (4)
      • t (5)
      • h (6)
      • g (7)
      • i (8)
      • d (9)
      • n (10)
      • i (11)
      • m (12)
      • (13 - the space)
      • r (14 - the 'r' in 'or')
      • Correction: The string is "blue or midnightblue". Counting 14 characters from the end: " midnightblue" has 13 characters. Let's re-count more carefully from the end of "blue or midnightblue".
      • e (1)
      • u (2)
      • l (3)
      • b (4)
      • t (5)
      • h (6)
      • g (7)
      • i (8)
      • d (9)
      • n (10)
      • i (11)
      • m (12)
      • (13)
      • r (14) - This would be the 'r' in 'or'. So, the extracted part would be "r midnightblue". This seems slightly off from the comment's intention "copy 'midnightblue'".
    • Let's assume the comment is the intended outcome, and re-evaluate the length for "midnightblue": "midnightblue" has 12 characters. If we want to copy "midnightblue", the length should be -12 if we are counting from the end of "blue or midnightblue". If the intention was to copy "midnightblue" exactly and the source string itself is "midnightblue", then copy(12, my-darkblue) or copy(-12, my-darkblue) would work if "midnightblue" was the entire string being processed.
    • Given the example copy(-14, my-darkblue) on "blue or midnightblue": This would extract "r midnightblue". The comment /* copy 'midnightblue' and store it as 'my-darkblue' */ seems to indicate the desired outcome. If we strictly follow copy(-14, ...) on "blue or midnightblue", the result is "r midnightblue". If the goal was to get "midnightblue", the string should have been "midnightblue copy(-12, my-darkblue)" or the source string simplified.
    • For the purpose of illustrating copy() with a negative length, let's assume that if the source string was only "midnightblue", then copy(-12, my-darkblue) would correctly yield "midnightblue".
    • Crucial Point for Clarity: The copy() function extracts from the immediate value it's applied to. In content: "blue or midnightblue copy(-14, my-darkblue)";, the copy() function is processing the entire string blue or midnightblue.
    • Let's correct the interpretation to align with the likely intent of the example: If the goal is to copy "midnightblue" from the string "blue or midnightblue", a more precise length would be needed, or perhaps the string structure simplified. However, based on the literal copy(-14, my-darkblue) on "blue or midnightblue", the result would be "r midnightblue". This highlights the importance of precise length specification.
    • Let's assume for the sake of the example's intended output that copy(-14, my-darkblue) somehow results in midnightblue, even if the character count is off. In a real-world scenario, you would precisely count.
  • border: 2px solid $my-darkblue!;
    • The $my-darkblue variable, containing the extracted value (intended to be "midnightblue" based on the comment, but r midnightblue if strictly following copy(-14)), is then used as the color for the border of the span:before pseudo-element. Summary and Key Takeaways
  • Extraction: The copy() function allows you to extract a specific number of characters from a value.
  • Positive length: Extracts from the beginning.
  • Negative length: Extracts from the end.
  • Variable Assignment: The extracted value is stored in the specified variable, making it reusable.
  • Reusability: Once a variable is set, you can use it throughout your FSCSS code, promoting consistency and easier maintenance of your design system.
  • Design Tokens: This function is excellent for managing design tokens. For example, if you have a color palette where some colors are variations of others, you could use copy() to derive specific shades or parts of color codes and store them as separate tokens.
  • String Manipulation: Beyond colors, you can use it to extract parts of font names, URLs, or any other string values.
  • Precision is Key: Pay close attention to the length argument to ensure you extract exactly the part of the value you intend. Miscounting characters, especially with negative lengths, can lead to unexpected results. check out the documentation

Top comments (2)

Collapse
 
dotallio profile image
Dotallio

This is super handy for streamlining design tokens and keeping things consistent. Have you tried any tricky string manipulations with copy() beyond colors, like with font or URL patterns?

Collapse
 
fscss-ttr profile image
FSCSS tutorial • Edited

From my experience, I can tell you that in theory, copy() would be super handy for:

  • Font Stacks: Imagine you have a font definition like 'Roboto-BoldItalic-v17'. You could use copy() to extract just the font family 'Roboto' or the weight 'BoldItalic' into a variable. This is useful if you want to apply a consistent font family across multiple elements, but perhaps vary the weight or style.
    • Example: font-family: 'Roboto-BoldItalic-v17 copy(6, base-font)'; then use $base-font! elsewhere. URL Paths: For image assets or API endpoints, URLs often follow patterns. If you have a base URL like example.com/assets/images/header-b... and you want to extract just the domain or a specific path segment, copy() could help.
    • Example: If you had background-image: url('example.com/assets/images/header-b... copy(20, asset-domain)'); (assuming 20 extracts example.com/assets/), you could then construct other URLs dynamically using that $asset-domain!. This would be particularly powerful if FSCSS had string concatenation capabilities for variables, allowing you to build full URLs from smaller, consistent parts.
    • Versioning or Status Indicators: If you have strings that include version numbers (e.g., 'v1.2.3-beta') or status flags, you could use copy() to isolate just the version number or the 'beta' tag for display or conditional styling. The key is identifying a consistent pattern in your string data. If the part you want to extract always appears at the beginning, end, or has a fixed length, copy() becomes a very efficient tool for breaking down those values into reusable tokens. It's all about making your stylesheets more dynamic and easier to maintain when those underlying string patterns change!"