Mansonry gallery with cusom code

Hi Everybody,
I’ve been working on an image gallery for a while and wanted to reference my existing Airtable source. This code creates a responsive masonry-style image gallery with hover effects. It’s displaying images in a visually appealing grid layout. On hover, each image reveals a description overlay. The gallery is suitable for various devices.

:grinning: Happy for feedback and adjustments!

To use this code:

  1. Replace the Airtable configuration (token, base ID, and table name) with your own.
  2. Ensure your Airtable has columns for image URLs and descriptions.
  3. Customize the styling as needed to fit your website’s design.

<div id="mansionGallery"></div>
<div id="debugInfo" style="display:none;"></div>

<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
<script src="https://unpkg.com/imagesloaded@5/imagesloaded.pkgd.min.js"></script>

<script>
const AIRTABLE_TOKEN = 'YOUR_AIRTABLE_TOKEN';
const AIRTABLE_BASE_ID = 'YOUR_BASE_ID';
const AIRTABLE_TABLE_NAME = 'YOUR_TABLE_NAME';

function createGallery(images) {
    const gallery = document.getElementById('mansionGallery');
    images.forEach((image) => {
        if (image.fields.ImageURL && image.fields.ImageURL[0] && image.fields.ImageURL[0].url) {
            const imgContainer = document.createElement('div');
            imgContainer.className = 'grid-item';
            
            const img = document.createElement('img');
            img.src = image.fields.ImageURL[0].url;
            img.alt = image.fields.Description || 'Image';
            
            const overlay = document.createElement('div');
            overlay.className = 'overlay';
            
            const description = document.createElement('p');
            description.className = 'description';
            description.textContent = image.fields.Description || '';
            
            overlay.appendChild(description);
            imgContainer.appendChild(img);
            imgContainer.appendChild(overlay);
            gallery.appendChild(imgContainer);
        }
    });

    imagesLoaded(gallery, function() {
        new Masonry(gallery, {
            itemSelector: '.grid-item',
            columnWidth: '.grid-item',
            percentPosition: true
        });
    });
}

fetch(`https://api.airtable.com/v0/${AIRTABLE_BASE_ID}/${AIRTABLE_TABLE_NAME}`, {
    headers: {
        'Authorization': `Bearer ${AIRTABLE_TOKEN}`
    }
})
.then(response => response.json())
.then(data => {
    createGallery(data.records);
})
.catch(error => {
    console.error('Error fetching records:', error);
});
</script>

<style>
#mansionGallery {
    width: 100%;
}
.grid-item {
    width: 33.333%;
    padding: 5px;
    box-sizing: border-box;
    position: relative;
    overflow: hidden;
}
.grid-item img {
    display: block;
    width: 100%;
    height: auto;
}
.overlay {
    position: absolute;
    top: 5px;
    left: 5px;
    right: 5px;
    bottom: 5px;
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}
.grid-item:hover .overlay {
    opacity: 1;
}
.description {
    color: white;
    text-align: center;
    padding: 10px;
    font-size: 14px;
}
@media (max-width: 900px) {
    .grid-item { width: 50%; }
}
@media (max-width: 600px) {
    .grid-item { width: 100%; }
}
</style>
2 Likes

Great work but one (very) important note:
You shouldn’t expose the Airtable API key or token in a frontend code. This is a real security breach :face_with_diagonal_mouth:.

Better is to display content on a static basis.

1 Like

@matthieu_chateau ; I appreciate you bringing this to attention. It’s feedback like this that helps maintain good security practices and protects users’ data. (I am too much of a Design thinker) You are 100% correct that exposing the Airtable API key or token in frontend code is a serious security breach. This oversight could potentially allow unauthorized access to the Airtable data, which is unacceptable in a production environment.

Your suggestion to display content on a static basis is excellent. This is indeed a much safer approach for handling this type of data presentation.

Hi Matthieu. Great point. How do we make this functionality possible without exposing an Airtable API key in the client code?

You will need to go through a middleware (some kind of an API server) where the key would be stored. You could achieve this with Make or Zapier which are connected to the same Airtable base. And to secure it further, you can limit which requests are accepted by the webhook based on the IP address or the email of the logged in user.

Softr (request with email) → Make (check if user exists, if so, fetch data and return to Softr)

Still not the most secure and limit it only to non-sensitive data.

Based on your insights, I would suggest the following approach to improve the gallery’s security and performance:

  1. Export the Airtable data as JSON.
  2. Download the JSON file.
  3. Store the JSON file on a server or in Dropbox.
  4. Reference this JSON file directly in the code, instead of connecting to the Airtable base. I am working on it,…

This method you’ve proposed offers several advantages:

  • It enhances security by removing direct Airtable access from the frontend.
  • It improves performance by eliminating API calls.

Still referencing (very restricted) Airtable in this example: Harald Palma Design Portfolio

Thank you Sahil! Appreciate the input.