Skip to content

Commit

Permalink
Feat: Removes possibility for multiple h1 on same page (#339)
Browse files Browse the repository at this point in the history
* Feat: Removes possibility for multiple h1 on same page

* Fix: ===

* Feat: Smarter typography component

Have fun with this one

---------

Co-authored-by: Niclas Norin <niclas.norin@helsingborg.se>
  • Loading branch information
NiclasNorin and Niclas Norin authored Oct 18, 2023
1 parent 585c9ac commit b998df9
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions source/php/Component/Typography/Typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class Typography extends \ComponentLibrary\Component\BaseController
{

private static $numberOfItems = 0;
private static $hasSeenH1 = null;
private static $headingsContext = null;

public function init() {
//Extract array for easy access (fetch only)
Expand All @@ -15,18 +15,47 @@ public function init() {
$this->data['isPromotedHeading'] = false;

//If this is the first heading of the page, promote it to h1
if ($autopromote === true) {
if (in_array($element, ['h1', 'h2', 'h3']) && self::$numberOfItems == 0) {
if (substr($element, 0, 2) == 'h1') {
self::$hasSeenH1 = true;
}

if (substr($element, 0, 1) == 'h') {
$this->data['element'] = $this->setMaxHeading($element);
}

if ($autopromote === true && !self::$hasSeenH1) {
if (in_array($element, ['h1', 'h2', 'h3'])) {
$this->data['isPromotedHeading'] = true;
$this->data['element'] = 'h1';
}

if (substr($element, 0, 1) == 'h') {
self::$numberOfItems++;
self::$hasSeenH1 = true;
}
}

//Variant
$this->data['classList'][] = $this->getBaseClass() . "__variant--" . $variant;
}

private function setMaxHeading($element) {
$headingsLevel = intval(substr($element, 1, 2));
if (self::$headingsContext === null) {
if ($element !== 'h2' && $element !== 'h1') {
$headingsLevel = 2;
self::$headingsContext = 2;
return 'h2';
}
} else {
if (self::$headingsContext == $headingsLevel && $headingsLevel != 1) {
self::$headingsContext = $headingsLevel;
} elseif (self::$hasSeenH1 && $headingsLevel == 1) {
self::$headingsContext = 2;
return 'h2';
} elseif ($headingsLevel - self::$headingsContext > 1) {
self::$headingsContext++;
return 'h' . strval(self::$headingsContext);
} else {
self::$headingsContext = $headingsLevel;
}
}
return $element;
}
}

0 comments on commit b998df9

Please sign in to comment.