Light Dark
Most modern operating systems let us control the preferred color theme. Luckily for us web developers, you can simply adjust the look of our sites by using the light-dark()
function.
This function allows you to define two colors for a property, in which your browser automatically show the appropriate value based on your preferences.
To enable support for the light-dark()
color function, the color-scheme
must have a value of light dark
, usually set on the :root
pseudo-class.
Ref: light-dark()
Trim Text
Many text truncation implementations often cut words abruptly in the middle, resulting in poor readability. The code below offers an elegant solution by ensuring the truncation occurs at word boundaries and adds ellipsis appropriately. It returns a tuple containing both the processed text and a flag indicating whether truncation was performed.
Ref: trimText
Cross Fade
The cross-fade()
CSS function can be used to blend two or more images at a defined transparency.
The cross-fade()
function takes a list of images with a percentage defining how much of each image is retained in terms of opacity when it is blended with the other images.
So we can use cross-fade()
to achieve the background image translucency effect.
Ref: cross-fade()
Create 2D Array
A 2D array is also known as a matrix. It's arranged in a table-like structure that consists of rows and columns. In TypeScript, we can create 2D arrays in several ways, Array.from()
being one of the most elegant solutions.
The Array.from()
method creates a shallow-copied array from an array-like or iterable object. It accepts two main parameters:
- An iterable or array-like object to convert
- A optional mapping function that transforms each element
Here's a utility function that creates a 2D array with specified dimensions and initial values:
Debugging Tips: When working with 2D arrays, use console.table()
for better visualization.
Ref: Array.from()
Document DesignMode
document.designMode
controls whether the entire document is editable. Valid values are "on"
and "off"
.
Convert String To Html
To convert an HTML string into real HTML or DOM, we can use the DOMParser
Web API using JavaScript. The DOMParser
helps us to parse HTML or XML string into real Document or DOM nodes.
There are other mime types we can use such as:
text/html
text/xml
application/xml
application/xhtml+xml
image/svg+xml
Ref:
React Conditional Rendering
Using Record
for conditional rendering provides a more elegant and maintainable solution compared to traditional methods like if/else or switch statements.
This approach offers several key advantages:
- Type Safety - The
Record
type ensures all possibleFruit
values have corresponding components at compile time. - Clean Code - Replaces verbose if/else or switch statements with a concise object lookup.
- Performance - Direct object lookups are more efficient than evaluating multiple conditions, and React only renders the relevant component.
- Readability - A single mapping object clearly visualizes all fruit-to-icon relationships.
- Scalability - Adding new fruit types only requires updating the type and mapping, not the component logic.
Ref: React Conditional Rendering With Type Safety and Exhaustive Checking