I would like to layout unicode glyphs similar to this:
There could be anywhere from 13 to say 100 items in the displayed collection (alphabets, abjads, abugidas, syllabaries, etc..), so could have a set with 99 items, or one with 16 items, etc..
How can you most beautiful (and automatically) layout the elements (so that it is also nice when responsive)? Here is what I have by default with flexbox:
Each item has a fixed height, and the width is responsive (but the same for each element).
The goal is to have every row have either an even number, or an odd number, of items. This way all vertical columns will line up (and it won't do what my animated gif is doing, where even/odd rows are intermingled).
How would you maybe do that?
I have a React component which does the Flexbox layout like <Grid minWidth={96} gap={8} maxColumns={5} breakpoints={[5, 3, 1]} />
, where breakpoints
are the number of items allowed per row, and maxColumns
is the max columns (breakpoints/maxColumn is kinda redundant, you can define either or, maybe I'll clean that up later). All this info might be irrelevant, but just pointing out just in case, it's my responsive grid component.
How can I somehow divide my total by x to figure out if it should be even or odd rows, and then set the maxColumns
or breakpoints
to whatever will be "most compact"? So for example:
The "beauty" of it might be subjective, and there's not a clear rule for what is most beautiful. Just looking for anything that doesn't easliy leave an orphaned single node at the end and then have 7 items on all other rows. Ideally we have:
maxRowCount - 2
(so if all rows have 7, the last row can have 5 or 7 only, or if all rows have 5, then the last row can have 3 or 5 only, etc.).minWidth: 96px
, and grows to fill the space. I can figure out how to make things responsive once we get the basics of this working, but assume a containerWidth
exists.So in my head, say we have containerWidth === 700px
. Then, 700/96 == 7+
, so perhaps 7 per row. If we have 99 items (for example), then 99 % 7 == 1
, so 7
won't work because we'll have 1 in the last row. 6
won't work because 99
is even, so try 5
? 99 % 5 == 4
, that won't work because 4
is even. So try 3
, 99 % 3 == 0
, so that would work as is, use that!
How would you implement something like that?
Here is an example that repeatedly tests if the number of columns matches your criteria
const ranges = [
[0x2600, 0x26FF], // Symbols
[0x2700, 0x27BF], // Dingbats
[0x1F300, 0x1F5FF] // Symbols and Pictographs
];
const randomUnicodeGlyph = () => {
const range = ranges[Math.floor(Math.random() * ranges.length)];
const codePoint = Math.floor(range[0] + Math.random() * (range[1] - range[0] + 1));
return String.fromCodePoint(codePoint);
};
// Generate a set of random Unicode glyphs
const generateGlyphSet = (length) => Array.from({ length }, randomUnicodeGlyph);
// Calculate the best number of columns per row
const calculateColumns = (totalItems, minItemWidth, containerWidth) => {
const minColumns = 3;
const maxColumns = 7;
let possibleColumns = Math.min(Math.floor(containerWidth / minItemWidth), maxColumns);
for (let columns = possibleColumns; columns >= minColumns; columns--) {
let rows = Math.floor(totalItems / columns);
let lastRowItems = totalItems % columns;
if (lastRowItems === 0 || lastRowItems >= (columns - 2)) {
return columns;
}
}
return minColumns; // fallback
};
// HTML table for the glyph set
const createTable = (glyphSet, containerWidth, minItemWidth) => {
const columns = calculateColumns(glyphSet.length, minItemWidth, containerWidth);
return `
<table class="glyph-table">
<tr>${glyphSet.map((glyph, index) =>
`<td>${glyph}</td>${(index + 1) % columns === 0 && (index + 1 !== glyphSet.length) ? '</tr><tr>' : ''}`
).join('')}
</tr>
</table><hr/>`;
};
// Generate three sets of glyphs with random lengths between 13 and 100
const sets = [
generateGlyphSet(16),
generateGlyphSet(26),
generateGlyphSet(99)
];
const containerWidth = 700;
const minItemWidth = 96;
document.body.innerHTML = sets.map(set => createTable(set, containerWidth, minItemWidth)).join('');
.glyph-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
.glyph-table td {
height: 96px;
text-align: center;
font-size: 24px;
padding: 3px;
}