Building a Custom Font Preview Tool for Shopify

Shopify August 17, 2024

Building a Custom Font Preview Tool for Shopify

 

Recently, I came across a Reddit post where a user asked how to implement a font preview tool inside a Shopify store. This inspired me to build a simple and customizable section that lets customers preview text in different fonts directly on a Shopify page. If you sell products like custom prints, signs, or t-shirts, this tool can be a valuable addition.

In this guide, I'll explain how the tool works and break down each part of the code to help you integrate it into your store. We'll focus on building and integrating the functionality within the Shopify CMS, with the styling hard-coded. For a deeper dive into styling, feel free to checkout my Custom Quiz Section where a build custom quiz that recommends products.

 

 

 

Understanding the Schema for Your Font Preview Tool

 

The schema is the backbone of your Shopify section, defining how the tool will be configured and displayed within the Shopify theme editor. It allows you to set up various options for font selection and text preview, making it easy for store managers to customize the tool according to their needs.

In this section, we’ll break down the schema into three main parts:

  1. Default Fonts Configuration: Here, you'll set up checkboxes for selecting default fonts that will be available for preview. This part of the schema allows you to define which fonts are pre-selected and which are optional.
  2. Custom Font Inputs: This section lets users specify additional custom fonts that can be added to the tool. It uses text input fields to allow for the flexibility of including fonts not listed in the default options.
  3. Preview Text Setup: Finally, you’ll define a default text field that allows users to input or modify the text that will be displayed in the preview tool. This helps in showing a realistic view of how different fonts render the text.

Default Fonts Configuration

The first part of the schema focuses on setting up default fonts using checkboxes. This section defines the fonts that will be available for selection in the preview tool. Here’s how it’s structured:

Explanation:

  • Type: Each font option is defined as a checkbox. This allows the store manager to select or deselect specific fonts.
  • ID: The id attribute uniquely identifies each font option in the schema. This is used to reference the font in the theme code and JavaScript.
  • Label: The label provides the name of the font as it will appear in the Shopify theme editor. This makes it clear which font each checkbox represents.
  • Default: The default attribute determines whether the font is pre-selected when the section is first added to the theme. For example, fonts like "Arial" and "Roboto" are set to true, meaning they will be checked by default, while others are set to false.

This setup allows store managers to easily enable or disable fonts for preview, giving them control over which fonts are showcased in the tool.

Next, let’s move on to the section that handles custom fonts.

Custom Fonts Input

The second part of the schema focuses on adding custom fonts. This section allows store managers to specify additional fonts that aren't included in the default list. Here’s how it's set up:

Explanation:

  • Type: Each custom font is defined as a text input. This allows the store manager to type in the names of additional fonts they want to include.
  • ID: The id attribute uniquely identifies each custom font input. This is used to reference these fonts in the theme code and dynamically update the font list.
  • Label: The label provides a descriptive name for each custom font input field in the Shopify theme editor. It helps store managers understand which field they are filling out.
  • Default: The default attribute provides a placeholder value for each input. This is useful for suggesting common fonts that can be easily replaced or customized.

With these text inputs, you give store managers the flexibility to add any font they wish to the preview tool. They can type in the names of additional fonts that aren’t part of the default list, ensuring the preview tool is adaptable to various needs.

Finally, let's explore how the schema handles the preview text that users will see in the font preview tool.

HTML Structure Breakdown

Now that we’ve set up the schema, let's dive into the HTML structure that creates the font preview tool. This section of the code sets up the user interface, including inputs for text and font selection, and a preview area where the text is displayed in the chosen font. Here’s a breakdown of how it all comes together:

Custom Text Input

Explanation:

  • Type: This is a standard text input field.

  • ID: The id="fontInput" attribute helps uniquely identify this input field, which will be used to dynamically update the preview text.

  • Placeholder: placeholder="Type your text here..." provides a hint to users about what they should enter.

  • Value: value="{{ settings.default_text }}" sets the initial text value to what’s defined in the schema, so users see a default message when they first load the page. 

 

Font Selection Dropdown

Explanation: 

  • Select Element: This dropdown allows users to choose from a list of fonts.
  • Font Lists:
    • default_fonts: This variable holds a list of predefined fonts split into an array. It’s a static list of common fonts.
    • additional_fonts: This starts as an empty array and will be populated with custom fonts provided in the schema.
  • Conditional Font Addition: The if statements check if each additional font field is not blank and, if so, add it to the additional_fonts array.
  • Combining Lists: all_fonts combines both default_fonts and additional_fonts arrays to ensure all fonts are available in the dropdown.
  • Looping Through Fonts: The for loop generates an <option> for each font. It sets the selected attribute if the font matches the default font.
  •  

    Font Preview Area

    Explanation: 

    • Preview Box: This <div> acts as a container for the text preview.
    • Preview Text: The <p> element displays the text using the font selected in the dropdown.
    • Inline Style: style="font-family: {{ settings.default_font }};" applies the selected font to the preview text. It dynamically updates based on the user's selection.

    This HTML setup ensures that users can interactively select different fonts and see their text displayed in real-time with those fonts. The integration with Shopify's Liquid templating language allows for dynamic content based on the schema settings.

     

    JavaScript Breakdown

    Now let’s dive into the JavaScript part of our font preview tool. This script makes the tool interactive by allowing users to see real-time changes as they input text or select different fonts. Here’s how each part of the code works:

    Event Listener for DOMContentLoaded and Variable Declarations

    This ensures that the script runs only after the entire HTML document has been loaded and parsed. This is important because it guarantees that all elements (like fontInput, fontSelect, and previewText) are available for manipulation.

     

  • fontInput: Refers to the text input where users type their custom text.
  • fontSelect: Refers to the dropdown menu where users select a font.
  • previewText: Refers to the paragraph element that displays the preview text.           
  • Loading the Default Font

    Explanation: When the page first loads, this function call ensures that the default font selected in the dropdown is loaded and applied to the preview text.

    Updating Preview Text on Input

  • Event Listener: Listens for the input event on the fontInput field. This event fires whenever the user types something into the field.
  • Functionality: Updates the previewText element with the text the user types. If the input field is empty, it defaults to the initial text provided in the schema ({{ settings.default_text }}).
  •  

    Updating Font on Dropdown Change

  • Event Listener: Listens for changes in the fontSelect dropdown menu.
  • Functionality:
    • Calls the loadFont function to load the selected font from Google Fonts.
    • Updates the fontFamily style of previewText to apply the selected font.
  • Explanation: Dynamically loads a font from Google Fonts.
  • Process:
    • Creates a new <link> element that points to the Google Fonts stylesheet URL for the selected font.
    • encodeURIComponent(fontName) ensures that the font name is correctly formatted for the URL.
    • Appends the <link> element to the document’s <head>, which triggers the browser to load and apply the font.
  • With this JavaScript code, the font preview tool becomes interactive and responsive. Users can see their text in different fonts instantly, making it easier to choose and visualize how text will appear in various styles. This integration enhances user experience, especially for Shopify stores offering customizable products.

     

    Conclusion

     

    With this guide, you’ve learned how to build a dynamic font preview tool for your Shopify store. This tool allows users to see real-time text previews in various fonts, making it a valuable addition to any store offering customizable products.

    By breaking down the schema, HTML, and JavaScript, we’ve ensured that each part of the tool is clear and easy to integrate. The schema allows you to manage font options and default text directly from Shopify’s CMS, the HTML structures the tool’s layout, and the JavaScript adds interactivity by updating the preview as users type and select different fonts.

    If you’d like to see the full code or need a reference for your own implementation, you can check out the GitHub repository where the complete project is available. Feel free to fork the repository and customize the tool to fit your store’s needs.

    Happy coding!

     

     

    img not found

    Chris Snowden

    Author

    Hi, I'm Christopher Snowden, a Shopify developer. I enjoy building and learning about e-commerce solutions that help businesses grow. With the rise of AI, I believe it is important for developers to understand more aspects of business and have a deeper understanding of how their skills translate to profit. Let's connect and collaborate! Follow me on social media to stay updated with my latest projects and insights.