| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
// -------------------------------------------------------------------------- |
| 24 |
// --- Utilities |
| 25 |
// -------------------------------------------------------------------------- |
| 26 |
|
| 27 |
/** |
| 28 |
@packageDocumentation |
| 29 |
@module dome/misc/utils |
| 30 |
*/ |
| 31 |
|
| 32 |
import type { CSSProperties } from 'react'; |
| 33 |
|
| 34 |
type Falsy = undefined | boolean | null | ''; |
| 35 |
|
| 36 |
export type ClassSpec = string | Falsy | { [cname: string]: true | Falsy }; |
| 37 |
|
| 38 |
/** |
| 39 |
Utility function to merge various HTML class properties |
| 40 |
into a `className` property. |
| 41 |
Class specifications can be made of: |
| 42 |
- a string, interpreted as a CSS class specification |
| 43 |
- an object, with keys corresponding to CSS class associated |
| 44 |
to true of falsy value. |
| 45 |
- any falsy value, which is discarded |
| 46 |
|
| 47 |
Example of usage: |
| 48 |
|
| 49 |
* ```ts |
| 50 |
* const className = classes( |
| 51 |
* 'my-base-class', |
| 52 |
* condition && 'my-class-when-condition', |
| 53 |
* { |
| 54 |
* 'my-class-1': cond-1, |
| 55 |
* 'my-class-2': cond-2, |
| 56 |
* 'my-class-3': cond-3, |
| 57 |
* } |
| 58 |
* ); |
| 59 |
* ``` |
| 60 |
|
| 61 |
*/ |
| 62 |
export function classes( |
| 63 |
...args: ClassSpec[] |
| 64 |
): string { |
| 65 |
const buffer: string[] = []; |
| 66 |
args.forEach((cla) => { |
| 67 |
if (cla) { |
| 68 |
if (typeof (cla) === 'string' && cla !== '') buffer.push(cla); |
| 69 |
else if (typeof (cla) === 'object') { |
| 70 |
const cs = Object.keys(cla); |
| 71 |
cs.forEach((c) => { if (cla[c]) buffer.push(c); }); |
| 72 |
} |
| 73 |
} |
| 74 |
}); |
| 75 |
return buffer.join(' '); |
| 76 |
} |
| 77 |
|
| 78 |
export type StyleSpec = Falsy | CSSProperties; |
| 79 |
|
| 80 |
/** |
| 81 |
Utility function to merge various CSS style properties |
| 82 |
into a single CSS style object. |
| 83 |
|
| 84 |
Each style specification can be made of a CSS object or (discarded) |
| 85 |
falsy values. |
| 86 |
Example of usage: |
| 87 |
|
| 88 |
* ```ts |
| 89 |
* const sty = styles( |
| 90 |
* { ... }, |
| 91 |
* cond-1 && { ... }, |
| 92 |
* cond-2 && { ... }, |
| 93 |
* ); |
| 94 |
* ``` |
| 95 |
|
| 96 |
*/ |
| 97 |
|
| 98 |
export function styles( |
| 99 |
...args: StyleSpec[] |
| 100 |
): CSSProperties | undefined { |
| 101 |
let empty = true; |
| 102 |
let buffer = {}; |
| 103 |
args.forEach((sty) => { |
| 104 |
if (sty && typeof (sty) === 'object') { |
| 105 |
empty = false; |
| 106 |
buffer = { ...buffer, ...sty }; |
| 107 |
} |
| 108 |
}); |
| 109 |
return (empty ? undefined : buffer); |
| 110 |
} |
| 111 |
|
| 112 |
// -------------------------------------------------------------------------- |