Skip to content

Commit

Permalink
Improve color schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Nov 26, 2024
1 parent ef8f922 commit 843ac75
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 7 deletions.
56 changes: 54 additions & 2 deletions src/Front/Library/Crazycolor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default class Crazycolor {
*
* @return {boolean}
*/
public applyTheme = ():boolean => {
public applyTheme = (target:HTMLElement = document.body):boolean => {

// Set result
let result = false;
Expand All @@ -118,7 +118,7 @@ export default class Crazycolor {
applyTheme(
this.theme,
{
target: document.body,
target: target,
dark: ColorSchema.getTheme() == "dark" ? true : false,
}
);
Expand All @@ -145,6 +145,58 @@ export default class Crazycolor {

}

/** Public static methods
******************************************************
*/

/**
* Scan Crazy Color
*
* Scan all el with attribute data-crazy-color
*
* @parm parent
* @returns {null|HTMLElement[]} all element found
*/
public static scanCrazyColor = (parent:HTMLElement|Document):null|HTMLElement[] => {

// Set result
let result:null|HTMLElement[] = null;

// Search els
let els = parent.querySelectorAll("[data-crazy-color]");

// Check els
if(els.length)

// Iteration els
els.forEach((el) => {

// Check el
if(el instanceof HTMLElement){

// Get attribute of el
var currentCrazyColor = el.dataset.crazyColor;

// Check currentCrazyColor
if(currentCrazyColor){

// Apply color on el
let currentColorInstance = new Crazycolor(currentCrazyColor);

// Apply on el
currentColorInstance.applyTheme(el);

}

}

});

// Return result
return result;

}

/** Private methods
******************************************************
*/
Expand Down
20 changes: 17 additions & 3 deletions src/Front/Library/Loader/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,18 +673,32 @@ export default class Page {
* @param options:LoaderPageOptions Options with all page details
* @return Promise<LoaderPageOptions>
*/
public static applyColorSchema = async(options:LoaderPageOptions):Promise<LoaderPageOptions> => {
public static applyColorSchema = async(options:LoaderPageOptions):Promise<LoaderPageOptions> => {

console.log(options);

// Get document
let doc = document;

// Scan crazy color
let result = Crazycolor.scanCrazyColor(doc);

// Check
if(result?.length)

// Set status
options = this.setStatus(options, "hasColorApplied", true);

// Check colors
if(options.status?.hasColor && options.color){
/* if(options.status?.hasColor && options.color){
// Apply theme
if(options.color.applyTheme())
// Set status
options = this.setStatus(options, "hasColorApplied", true);
}
} */

// Return options
return options
Expand Down
69 changes: 67 additions & 2 deletions src/Library/Html/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use CrazyPHP\Library\File\Config;
use CrazyPHP\Library\Html\Head;
use CrazyPHP\Library\File\File;
use CrazyPHP\Library\String\Color;
use CrazyPHP\Model\Context;

/**
Expand Down Expand Up @@ -408,7 +409,7 @@ public function setLanguage(?string $language = null, array $keys = ["LANG", "LA
return $this;

}

/** Public methods | body
******************************************************
*/
Expand Down Expand Up @@ -451,6 +452,30 @@ public function setHead(string $config = "main", array $attributes = []):self {
*/
public function setBody(string|array|null $attributes = null):self {

# Push color attribute
$this->_pushColorSchema($attributes);

# Set element
$this->setElement("html", $attributes, "body");

# Return current instance
return $this;

}

/**
* Set Body Color Schema
*
* Set Body Tag
*
* @param ?string $source For override current colorschema
* @return self
*/
public function setBodyColorSchema(?string $source = null):self {

# Push color attribute
$this->_pushColorSchema($attributes, $source);

# Set element
$this->setElement("html", $attributes, "body");

Expand All @@ -470,11 +495,13 @@ public function setBody(string|array|null $attributes = null):self {
public function setBodyContent(?string $content = ""):self {

# Check content
if($content !== null)
if($content !== null){

# Set element
$this->setElement("html.body", $content, null);

}

# Return current instance
return $this;

Expand Down Expand Up @@ -783,6 +810,44 @@ private function _setHash(string &$input):void {

}

/**
* Push Color schema on attributes
*
* @param mixed &$attributes attributes to update
* @param ?string $source to override default color schema
* @return void
*/
private function _pushColorSchema(mixed &$attributes, ?string $source = null):void {

# Get color source
$appSource = Config::getValue("Style.materialDynamicColors.source");

# Push language in response
$attributes["data-default-color"] = $appSource;

# Check source
if(!$source){

# Get url parameter
$source = $appSource;

}else
# Check source
if($source && Color::isValid($source)){

# check attributes
if(!is_array($attributes))

# Convert to array
$attributes = [];

# Set language in response
$attributes["data-crazy-color"] = $source;

}

}

/** Public constants
******************************************************
*/
Expand Down
31 changes: 31 additions & 0 deletions src/Library/String/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
use OzdemirBurak\Iris\Color\Factory;
use OzdemirBurak\Iris\BaseColor;
use OzdemirBurak\Iris\Exceptions\AmbiguousColorString;
use OzdemirBurak\Iris\Exceptions\InvalidColorException;

/**
* Color
Expand Down Expand Up @@ -228,4 +230,33 @@ public static function randomHex():string {

}

/**
* Is Valid
*
* @param string $input
* @return bool
*/
public static function isValid(string $input):bool {

# Set result
$result = true;

# Try
try{

# New color
new Color($input);

# Catch error
}catch(AmbiguousColorString|InvalidColorException $e){

$result = false;

}

# Return result
return $result;

}

}
27 changes: 27 additions & 0 deletions tests/Library/String/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ public static function tearDownAfterClass():void {
******************************************************
*/

/**
* Test Color Valid
*
* @return void
*/
public static function testColorValid():void {

# Check hex
static::assertTrue(Color::isValid("#a2837a"));

# Check rgb
static::assertTrue(Color::isValid("rgb(255, 0, 255)"));

}

/**
* Test Color Invalid
*
* @return void
*/
public static function testColorInvalid():void {

# Check valid
static::assertFalse(Color::isValid("toto"));

}

/**
* test Color Package
*
Expand Down

0 comments on commit 843ac75

Please sign in to comment.