Skip to content

Commit

Permalink
feat(@utilities): add margin, padding and spacing classes (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter authored Jun 5, 2024
1 parent fee61db commit 8a8c357
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 0 deletions.
2 changes: 2 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"ci": "deno task lint && deno task fmt:check && deno task build",
"serve": "deno run --allow-net --allow-read --allow-env --allow-sys --watch app/mod.ts serve",
"build": "deno run --allow-net --allow-read --allow-env --allow-sys --allow-write=.pages,dist app/mod.ts build",
"generate": "deno task generate:utilities",
"generate:utilities": "deno run --allow-write=styles/@utilities/mod+generated.css styles/@utilities/generate.ts",
"lint": "deno lint",
"fmt": "deno task fmt:ts && deno task fmt:css",
"fmt:check": "deno task fmt:ts:check && deno task fmt:css:check",
Expand Down
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions styles/@utilities/classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ <h2 id="utilities-classes">
Consider using (or switching to) a utility-first CSS framework if you find the provided utility classes lacking or too limited.
</p>
</div>
<h3 id="utilities-classes-list">
<a href="#utilities-classes-list">Utilities classes</a>
</h3>
<ul>
<li>
Text <a href="https://developer.mozilla.org/docs/Web/CSS/font-weight">weight</a>, <a href="https://developer.mozilla.org/docs/Web/CSS/font-style">style</a>, <a href="https://developer.mozilla.org/docs/Web/CSS/text-decoration">decoration</a>, <a href="https://developer.mozilla.org/docs/Web/CSS/text-transform">transform</a>, <a href="https://developer.mozilla.org/docs/Web/CSS/text-align">align</a>, and <a href="https://developer.mozilla.org/docs/Web/CSS/font-family">font</a>.
Expand Down Expand Up @@ -170,4 +173,44 @@ <h2 id="utilities-classes">
</ul>
</li>
</ul>
<h3 id="utilities-classes-spacing">
<a href="#utilities-classes-spacing">Spacing classes</a>
</h3>
<p>
The following classes are numbered and the following values are supported in place of <code>*</code>:<br>
<code data-hl="ts">0</code>, <code data-hl="ts">.125</code>, <code data-hl="ts">.25</code>, <code data-hl="ts">.5</code>, <code data-hl="ts">.75</code>, <code data-hl="ts">1</code>, <code data-hl="ts">1.25</code>, <code data-hl="ts">1.5</code>, <code data-hl="ts">1.75</code>, <code data-hl="ts">2</code>, <code data-hl="ts">3</code>, <code data-hl="ts">4</code>,
</p>
<ul>
<li>
<a href="https://developer.mozilla.org/docs/Web/CSS/margin">Margins</a>
<ul>
<li><code data-hl="css">.m-*</code> sets <code data-hl="css">margin: *rem;</code></li>
<li><code data-hl="css">.mx-*</code> sets <code data-hl="css">margin-left: *rem; margin-right: *rem;</code></li>
<li><code data-hl="css">.my-*</code> sets <code data-hl="css">margin-top: *rem; margin-bottom: *rem;</code></li>
<li><code data-hl="css">.mt-*</code> sets <code data-hl="css">margin-top: *rem;</code></li>
<li><code data-hl="css">.mb-*</code> sets <code data-hl="css">margin-bottom: *rem;</code></li>
<li><code data-hl="css">.ml-*</code> sets <code data-hl="css">margin-left: *rem;</code></li>
<li><code data-hl="css">.mr-*</code> sets <code data-hl="css">margin-right: *rem;</code></li>
<li>
<code data-hl="css">&</code>
<ul>
<li><code data-hl="css">&.spacing-x-* &gt; * + *</code> sets <code data-hl="css">margin-left: *rem;</code></li>
<li><code data-hl="css">&.spacing-y-* &gt; * + *</code> sets <code data-hl="css">margin-top: *rem;</code></li>
</ul>
</li>
</ul>
</li>
<li>
<a href="https://developer.mozilla.org/docs/Web/CSS/padding">Paddings</a>
<ul>
<li><code data-hl="css">.p-*</code> sets <code data-hl="css">padding: *rem;</code></li>
<li><code data-hl="css">.px-*</code> sets <code data-hl="css">padding-left: *rem; padding-right: *rem;</code></li>
<li><code data-hl="css">.py-*</code> sets <code data-hl="css">padding-top: *rem; padding-bottom: *rem;</code></li>
<li><code data-hl="css">.pt-*</code> sets <code data-hl="css">padding-top: *rem;</code></li>
<li><code data-hl="css">.pb-*</code> sets <code data-hl="css">padding-bottom: *rem;</code></li>
<li><code data-hl="css">.pl-*</code> sets <code data-hl="css">padding-left: *rem;</code></li>
<li><code data-hl="css">.pr-*</code> sets <code data-hl="css">padding-right: *rem;</code></li>
</ul>
</li>
</ul>
</section>
49 changes: 49 additions & 0 deletions styles/@utilities/generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { fromFileUrl } from "jsr:@std/path/from-file-url"
const rules = ["/* This file is auto-generated, please do not edit manually */"]
const values = [0, .125, .25, .5, .75, 1, 1.25, 1.5, 1.75, 2, 3, 4].map((value) => `${value}`.replace(/^0./, "."))

// Margin and padding utilities
for (const property of ["margin", "padding"]) {
for (const direction of ["", "x", "y", "top", "right", "bottom", "left"]) {
for (const value of values) {
let rule = `.${property.charAt(0)}${direction.charAt(0)}-${value.replace(".", "\\.")} { `
switch (direction) {
case "":
rule += `${property}: ${value}rem;`
break
case "x":
rule += `${property}-left: ${value}rem; ${property}-right: ${value}rem;`
break
case "y":
rule += `${property}-top: ${value}rem; ${property}-bottom: ${value}rem;`
break
default:
rule += `${property}-${direction}: ${value}rem;`
}
rule += " }"
rules.push(rule)
}
}
}

// Spacing utilities
for (const property of ["spacing"]) {
for (const direction of ["x", "y"]) {
for (const value of values) {
let rule = `.${property}-${direction.charAt(0)}-${value.replace(".", "\\.")} > * + * { `
switch (direction) {
case "x":
rule += `margin-left: ${value}rem;`
break
case "y":
rule += `margin-top: ${value}rem;`
break
}
rule += " }"
rules.push(rule)
}
}
}

// Save generated CSS
await Deno.writeTextFile(fromFileUrl(import.meta.resolve("./mod+generated.css")), rules.join("\n"))
193 changes: 193 additions & 0 deletions styles/@utilities/mod+generated.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/* This file is auto-generated, please do not edit manually */
.m-0 { margin: 0rem; }
.m-\.125 { margin: .125rem; }
.m-\.25 { margin: .25rem; }
.m-\.5 { margin: .5rem; }
.m-\.75 { margin: .75rem; }
.m-1 { margin: 1rem; }
.m-1\.25 { margin: 1.25rem; }
.m-1\.5 { margin: 1.5rem; }
.m-1\.75 { margin: 1.75rem; }
.m-2 { margin: 2rem; }
.m-3 { margin: 3rem; }
.m-4 { margin: 4rem; }
.mx-0 { margin-right: 0rem; margin-left: 0rem; }
.mx-\.125 { margin-right: .125rem; margin-left: .125rem; }
.mx-\.25 { margin-right: .25rem; margin-left: .25rem; }
.mx-\.5 { margin-right: .5rem; margin-left: .5rem; }
.mx-\.75 { margin-right: .75rem; margin-left: .75rem; }
.mx-1 { margin-right: 1rem; margin-left: 1rem; }
.mx-1\.25 { margin-right: 1.25rem; margin-left: 1.25rem; }
.mx-1\.5 { margin-right: 1.5rem; margin-left: 1.5rem; }
.mx-1\.75 { margin-right: 1.75rem; margin-left: 1.75rem; }
.mx-2 { margin-right: 2rem; margin-left: 2rem; }
.mx-3 { margin-right: 3rem; margin-left: 3rem; }
.mx-4 { margin-right: 4rem; margin-left: 4rem; }
.my-0 { margin-top: 0rem; margin-bottom: 0rem; }
.my-\.125 { margin-top: .125rem; margin-bottom: .125rem; }
.my-\.25 { margin-top: .25rem; margin-bottom: .25rem; }
.my-\.5 { margin-top: .5rem; margin-bottom: .5rem; }
.my-\.75 { margin-top: .75rem; margin-bottom: .75rem; }
.my-1 { margin-top: 1rem; margin-bottom: 1rem; }
.my-1\.25 { margin-top: 1.25rem; margin-bottom: 1.25rem; }
.my-1\.5 { margin-top: 1.5rem; margin-bottom: 1.5rem; }
.my-1\.75 { margin-top: 1.75rem; margin-bottom: 1.75rem; }
.my-2 { margin-top: 2rem; margin-bottom: 2rem; }
.my-3 { margin-top: 3rem; margin-bottom: 3rem; }
.my-4 { margin-top: 4rem; margin-bottom: 4rem; }
.mt-0 { margin-top: 0rem; }
.mt-\.125 { margin-top: .125rem; }
.mt-\.25 { margin-top: .25rem; }
.mt-\.5 { margin-top: .5rem; }
.mt-\.75 { margin-top: .75rem; }
.mt-1 { margin-top: 1rem; }
.mt-1\.25 { margin-top: 1.25rem; }
.mt-1\.5 { margin-top: 1.5rem; }
.mt-1\.75 { margin-top: 1.75rem; }
.mt-2 { margin-top: 2rem; }
.mt-3 { margin-top: 3rem; }
.mt-4 { margin-top: 4rem; }
.mr-0 { margin-right: 0rem; }
.mr-\.125 { margin-right: .125rem; }
.mr-\.25 { margin-right: .25rem; }
.mr-\.5 { margin-right: .5rem; }
.mr-\.75 { margin-right: .75rem; }
.mr-1 { margin-right: 1rem; }
.mr-1\.25 { margin-right: 1.25rem; }
.mr-1\.5 { margin-right: 1.5rem; }
.mr-1\.75 { margin-right: 1.75rem; }
.mr-2 { margin-right: 2rem; }
.mr-3 { margin-right: 3rem; }
.mr-4 { margin-right: 4rem; }
.mb-0 { margin-bottom: 0rem; }
.mb-\.125 { margin-bottom: .125rem; }
.mb-\.25 { margin-bottom: .25rem; }
.mb-\.5 { margin-bottom: .5rem; }
.mb-\.75 { margin-bottom: .75rem; }
.mb-1 { margin-bottom: 1rem; }
.mb-1\.25 { margin-bottom: 1.25rem; }
.mb-1\.5 { margin-bottom: 1.5rem; }
.mb-1\.75 { margin-bottom: 1.75rem; }
.mb-2 { margin-bottom: 2rem; }
.mb-3 { margin-bottom: 3rem; }
.mb-4 { margin-bottom: 4rem; }
.ml-0 { margin-left: 0rem; }
.ml-\.125 { margin-left: .125rem; }
.ml-\.25 { margin-left: .25rem; }
.ml-\.5 { margin-left: .5rem; }
.ml-\.75 { margin-left: .75rem; }
.ml-1 { margin-left: 1rem; }
.ml-1\.25 { margin-left: 1.25rem; }
.ml-1\.5 { margin-left: 1.5rem; }
.ml-1\.75 { margin-left: 1.75rem; }
.ml-2 { margin-left: 2rem; }
.ml-3 { margin-left: 3rem; }
.ml-4 { margin-left: 4rem; }
.p-0 { padding: 0rem; }
.p-\.125 { padding: .125rem; }
.p-\.25 { padding: .25rem; }
.p-\.5 { padding: .5rem; }
.p-\.75 { padding: .75rem; }
.p-1 { padding: 1rem; }
.p-1\.25 { padding: 1.25rem; }
.p-1\.5 { padding: 1.5rem; }
.p-1\.75 { padding: 1.75rem; }
.p-2 { padding: 2rem; }
.p-3 { padding: 3rem; }
.p-4 { padding: 4rem; }
.px-0 { padding-right: 0rem; padding-left: 0rem; }
.px-\.125 { padding-right: .125rem; padding-left: .125rem; }
.px-\.25 { padding-right: .25rem; padding-left: .25rem; }
.px-\.5 { padding-right: .5rem; padding-left: .5rem; }
.px-\.75 { padding-right: .75rem; padding-left: .75rem; }
.px-1 { padding-right: 1rem; padding-left: 1rem; }
.px-1\.25 { padding-right: 1.25rem; padding-left: 1.25rem; }
.px-1\.5 { padding-right: 1.5rem; padding-left: 1.5rem; }
.px-1\.75 { padding-right: 1.75rem; padding-left: 1.75rem; }
.px-2 { padding-right: 2rem; padding-left: 2rem; }
.px-3 { padding-right: 3rem; padding-left: 3rem; }
.px-4 { padding-right: 4rem; padding-left: 4rem; }
.py-0 { padding-top: 0rem; padding-bottom: 0rem; }
.py-\.125 { padding-top: .125rem; padding-bottom: .125rem; }
.py-\.25 { padding-top: .25rem; padding-bottom: .25rem; }
.py-\.5 { padding-top: .5rem; padding-bottom: .5rem; }
.py-\.75 { padding-top: .75rem; padding-bottom: .75rem; }
.py-1 { padding-top: 1rem; padding-bottom: 1rem; }
.py-1\.25 { padding-top: 1.25rem; padding-bottom: 1.25rem; }
.py-1\.5 { padding-top: 1.5rem; padding-bottom: 1.5rem; }
.py-1\.75 { padding-top: 1.75rem; padding-bottom: 1.75rem; }
.py-2 { padding-top: 2rem; padding-bottom: 2rem; }
.py-3 { padding-top: 3rem; padding-bottom: 3rem; }
.py-4 { padding-top: 4rem; padding-bottom: 4rem; }
.pt-0 { padding-top: 0rem; }
.pt-\.125 { padding-top: .125rem; }
.pt-\.25 { padding-top: .25rem; }
.pt-\.5 { padding-top: .5rem; }
.pt-\.75 { padding-top: .75rem; }
.pt-1 { padding-top: 1rem; }
.pt-1\.25 { padding-top: 1.25rem; }
.pt-1\.5 { padding-top: 1.5rem; }
.pt-1\.75 { padding-top: 1.75rem; }
.pt-2 { padding-top: 2rem; }
.pt-3 { padding-top: 3rem; }
.pt-4 { padding-top: 4rem; }
.pr-0 { padding-right: 0rem; }
.pr-\.125 { padding-right: .125rem; }
.pr-\.25 { padding-right: .25rem; }
.pr-\.5 { padding-right: .5rem; }
.pr-\.75 { padding-right: .75rem; }
.pr-1 { padding-right: 1rem; }
.pr-1\.25 { padding-right: 1.25rem; }
.pr-1\.5 { padding-right: 1.5rem; }
.pr-1\.75 { padding-right: 1.75rem; }
.pr-2 { padding-right: 2rem; }
.pr-3 { padding-right: 3rem; }
.pr-4 { padding-right: 4rem; }
.pb-0 { padding-bottom: 0rem; }
.pb-\.125 { padding-bottom: .125rem; }
.pb-\.25 { padding-bottom: .25rem; }
.pb-\.5 { padding-bottom: .5rem; }
.pb-\.75 { padding-bottom: .75rem; }
.pb-1 { padding-bottom: 1rem; }
.pb-1\.25 { padding-bottom: 1.25rem; }
.pb-1\.5 { padding-bottom: 1.5rem; }
.pb-1\.75 { padding-bottom: 1.75rem; }
.pb-2 { padding-bottom: 2rem; }
.pb-3 { padding-bottom: 3rem; }
.pb-4 { padding-bottom: 4rem; }
.pl-0 { padding-left: 0rem; }
.pl-\.125 { padding-left: .125rem; }
.pl-\.25 { padding-left: .25rem; }
.pl-\.5 { padding-left: .5rem; }
.pl-\.75 { padding-left: .75rem; }
.pl-1 { padding-left: 1rem; }
.pl-1\.25 { padding-left: 1.25rem; }
.pl-1\.5 { padding-left: 1.5rem; }
.pl-1\.75 { padding-left: 1.75rem; }
.pl-2 { padding-left: 2rem; }
.pl-3 { padding-left: 3rem; }
.pl-4 { padding-left: 4rem; }
.spacing-x-0 > * + * { margin-left: 0rem; }
.spacing-x-\.125 > * + * { margin-left: .125rem; }
.spacing-x-\.25 > * + * { margin-left: .25rem; }
.spacing-x-\.5 > * + * { margin-left: .5rem; }
.spacing-x-\.75 > * + * { margin-left: .75rem; }
.spacing-x-1 > * + * { margin-left: 1rem; }
.spacing-x-1\.25 > * + * { margin-left: 1.25rem; }
.spacing-x-1\.5 > * + * { margin-left: 1.5rem; }
.spacing-x-1\.75 > * + * { margin-left: 1.75rem; }
.spacing-x-2 > * + * { margin-left: 2rem; }
.spacing-x-3 > * + * { margin-left: 3rem; }
.spacing-x-4 > * + * { margin-left: 4rem; }
.spacing-y-0 > * + * { margin-top: 0rem; }
.spacing-y-\.125 > * + * { margin-top: .125rem; }
.spacing-y-\.25 > * + * { margin-top: .25rem; }
.spacing-y-\.5 > * + * { margin-top: .5rem; }
.spacing-y-\.75 > * + * { margin-top: .75rem; }
.spacing-y-1 > * + * { margin-top: 1rem; }
.spacing-y-1\.25 > * + * { margin-top: 1.25rem; }
.spacing-y-1\.5 > * + * { margin-top: 1.5rem; }
.spacing-y-1\.75 > * + * { margin-top: 1.75rem; }
.spacing-y-2 > * + * { margin-top: 2rem; }
.spacing-y-3 > * + * { margin-top: 3rem; }
.spacing-y-4 > * + * { margin-top: 4rem; }

0 comments on commit 8a8c357

Please sign in to comment.