Snippet Manager
Learn how to build a snippet manager extension that lets developers store and organize code snippets directly within the Replit workspace.
Create a Snippet Manager
In this tutorial, we will build a snippet manager extension with React. A snippet manager is a tool used for storing and organizing commonly used pieces of code or text that can be quickly accessed.
Prequisites
This tutorial assumes that you have a basic knowledge and understanding of web development and React.
Set up your Replit App
- Fork the Replit React Extension Template.
- Install the
react-feather
package withnpm install react-feather
.
Configure the manifest file
Configure the title and description in public/extension.json
(the Extension manifest file).
Add the tools
property to extension.json
and provide a tool that handles the /
route of your application. This will allow your extension to appear as a tool in the sidebar.
Build the Snippet Manager
Import the following dependencies in src/App.jsx
Remove all the existing code from the App
function and add state variables snippets
and newSnippetValue
. snippets
will store all of the snippets you’ve created and newSnippetValue
will be a string for when you create a new snippet.
Initialize the Handshake
Initialize the handshake and derive the status
and error
properties from the useReplit hook
within the App
function.
The status
property is an enumerated value indicating whether the handshake connection with Replit is loading
, ready
, or has resulted in an error
.
Handle Handshake Statuses
Handle the loading
and error
statuses from the useReplit
hook. If the Extension is neither loading nor has resulted in an error, the main content will be rendered.
Paste the following CSS code into src/App.css
to apply basic styling to your exstension. If you refresh the extension, the snippets you’ve added will be saved.
Snippet Creation
Create an empty function insertSnippet
within the App
function.
Add the following JSX code into the App
function after the h1
tag. This will be a basic form to create a new snippet.
Define an asynchronous function updateSnippets
in App
. This will allow us to update the snippets
state variable and save it using the replDb module at the same time.
:::note Values saved to a Replit App’s database get automatically URI-decoded so encoding has to happen twice. Replit App Databases can only store key-values as strings. Read More. :::
Back in the insertSnippet
function, use the updateSnippets
function to insert and save newSnippetValue
to the list of snippets.
Add the following CSS code into src/App.css
to style the snippet creation form:
Load and list the snippets
Define an asynchronous function loadSnippets
in App
. This function will fetch all the snippets from the Replit database and update the snippets
state variable.
Call the useReplitEffect
hook to run the loadSnippets
function once, when Replit successfully connects to your Extension.
Add the following code to the main UI after the .create-snippet-form
element. Display each snippet as a paragraph from the snippet
state variable.
Install your extension, start creating some snippets, watch as they appear in realtime.
The Snippet Component
You will be able to copy, edit, and delete snippets from the Snippet
component with the click of a button.
Create a file src/components/Snippet.jsx
and import the following dependencies:
Create and export the Snippet
component. The props declared in this component are as follows:
content
is the value of the snippetindex
is a number signifying the index of the snippetsnippets
is a full list of all the snippets in the databaseupdateSnippets
is the asynchronous function passed down from theApp
component
Add two state variables isEditing
and value
. isEditing
indicates whether the snippet is being edited and value
is the new value which will be used when editing the snippet, before it is saved.
Add a useEffect hook to set value
to content
whenever content
updates. The prop will change when snippets get edited and deleted.
Create a function copyToClipboard
. Handle the asynchronous clipboard event with the .then()
and .catch()
methods. Use the messages
module to display whether the action was successful or not.
Define a function saveEdit
. This will update the current snippet to reflect the value
state variable and save it to the database.
Add a function deleteSnippet
. After it is confirmed that the user wants to delete the snippet, remove the snippet from both the application state and the database.
Complete the UI of the Snippet
component with the following JSX code. If the snippet is being edited, a different component will be shown.
Apply the following CSS to src/App.css
:
Display the snippets
In App.jsx
, import the Snippet
component.
Update the .snippet-list
div to render the new component.
Finally, style the snippet list with the following CSS:
The Snippet Manager extension is now complete! Install it, open the Tools section on the sidebar, and select the Snippet Manager extension. You can now easily save and access your favorite snippets directly within your Replit App.
Was this page helpful?