Chakra-UI


Code

|

Chakra Docs

|

⦾ When developing a website or a web application that frequently presents users with code snippets, highlighting and properly formatting the code becomes crucial. It increases readability, understanding, and the overall aesthetic of your site. This is where the Code component from Chakra UI shines. In this post, we'll delve into the usage, properties, theming, and much more, of this handy tool.

The Code component in Chakra UI is designed to display inline code. Built from Chakra's foundational Box component, it comes with a mono font family – a commCodeisplaying code. The Code component is a seamless way to incorporate code in the flow of your text or UI elements, making it an excellent choice for developer blogs, documentation, or any context where you want to reference code inline.

The Code component from Chakra UI offers an easy-to-implement, customizable way to integrate code snippets within your website or web app's UI. With customizable color schemes and the readability of monospace fonts, presenting code snippets in an aesthetic and user-friendly way is very simple and straightforward.

The Code component can be imported as follows:

Examples

Basic Usage


⦾ Using the Code component is as simple as wrapping the code you wish to display within the <Code> tags.
const greeting = "Whazzup, world!?!?";console.log(greeting);

Code

Code and Colors


⦾ One of the really useful features of the Code component is the ability to change the color scheme of the code display by passing the colorScheme prop. This is especially helpful when you want to color-code your commands, statements or messages for clarity, or simply to align with your website's theme.

The following example shows all the colorScheme possibilities:

console.log("Code with a teal colorScheme.")console.log("Code with a purple colorScheme.")console.log("Code with a orange colorScheme.")console.log("Code with a whiteAlpha colorScheme.")console.log("Code with a blackAlpha colorScheme.")console.log("Code with a red colorScheme.")console.log("Code with a green colorScheme.")console.log("Code with a cyan colorScheme.")console.log("Code with a pink colorScheme.")console.log("Code with a linkedin colorScheme.")console.log("Code with a facebook colorScheme.")console.log("Code with a messenger colorScheme.")console.log("Code with a whatsapp colorScheme.")console.log("Code with a twitter colorScheme.")console.log("Code with a telegram colorScheme.")

Code

Code and Font Sizes


⦾ The Code component in Chakra UI uses a monospace font by default, but what if you want to change the font size? This can be easily accomplished by using the fontSize prop with the Code component.
const size = 'extra small';const size = 'small';const size = 'medium';const size = 'large';const size = 'extra large';

Code

Did you know?

Creative Idea No. 1

Adding hover effects: While Code is a component mainly used for displaying inline code, you can also use it for other purposes, like adding hover effects to text to provide additional information or context.
Hover over me

Code

Creative Idea No. 2

Using with Tooltip for additional context: Sometimes, your code or text inside the Code component might need additional context. You can pair the Code component with the Tooltipcomponent to give more information when users hover over it.
Hover for info

Code

Creative Idea No. 3

Aesthetic Use: You can use the Code component to highlight or draw attention to certain parts of non-code text by incorporating it within the Text or Headingcomponents. In this example, the Code component is used to highlight the word "settings" in a sentence. This makes it visually distinct, drawing the reader's attention to that particular word, making your UI more dynamic and engaging, and also improving user experience by guiding the user's attention to where it's needed most.

Please make sure to check the settings before proceeding.

Code

copyright © 2023 IHeartComponents | evanmarie.com

Chakra-UIRemix

Special thanks to Stefan Bohacek / Generative Placeholders.