Giter Club home page Giter Club logo

Comments (3)

Boldewyn avatar Boldewyn commented on June 26, 2024 1

Hm. I’m a bit at a loss at what should be fixed/changed at the library level. Given that the concrete styles on any given page can have all kind of side effects, it seems futile to cater for them one by one.

There is already the possibility to adapt the way error message nodes are attached to the DOM: https://hyperform.js.org/examples.html#file=global_message
Maybe that’s a better way to handle the issue? I.e., place the message somewhere else in the DOM outside the grid?

In that case the specifics of Hyperform’s API are detailed at https://hyperform.js.org/docs/high_level_api.html#move-around-or-manipulate-the-error-message

If that doesn’t fix your problem I’d love to hear how I could concretely help you in this case.

from hyperform.

Rhys-T avatar Rhys-T commented on June 26, 2024 1

Yeah, that makes sense. There's only so much the library can do to keep the page from breaking it. 😄

Thanks for pointing out the renderer hooks, though - I hadn't taken in the fact that those existed. I think I have some ideas on how to use those to get the warnings to show up in the right place. I'll reply here again once I've had a chance to work out the details and test it out.

from hyperform.

Rhys-T avatar Rhys-T commented on June 26, 2024 1

The attachWarning renderer hook was exactly what I needed. I was able to implement custom positioning for the warnings that works correctly in grids.

Details of the workaround I'm using, in case anyone else is trying to use Hyperform with grids

HTML (example)

<form id="someForm" action="https://foo.bar.example/whatever" method="post">
	<label for="name">Name:</label>
	<input type="text" id="name" name="name" required>
	<label for="email">Email address:</label>
	<input type="email" id="email" name="email" required>
	<button type="submit">Submit</button>
</form>

JavaScript

hyperform(window);
/*
Workaround for <https://github.com/hyperform/hyperform/issues/127>.

Hyperform just sets its warnings as position: absolute without
setting coordinates for them. Normally, that just puts it
'wherever it would be' - i.e. right below the input - but when
the input is a grid item, the warning ends up defaulting to the
upper-left-corner of the grid.

With this renderer hook installed, warnings for grid-based inputs
will instead end up inside wrapper elements which are assigned to
the same grid areas as the corresponding inputs. The warnings are
positioned so that their top edges sit at the bottom of the grid
cells, thus placing them below the inputs.

Limitations:
- This code assumes you're positioning the inputs with grid-area,
  not numeric row/column or auto-layout. Can probably be adapted.
- If the input is smaller than the grid cell, the warning may
  appear too far away from it. Not sure what to do about that.
- It relies on classList. That should be supported in just about
  anything that supports grid, but if not, it can be changed to
  just use className.
*/
const hfWarningGridWrapperClass = 'hfWarningGridWrapper';
const hfGridAreaProperty = '--hf-grid-area';
hyperform.setRenderer('attachWarning', (warning, input) => {
	/*
	getComputedStyle's behavior for shorthand properties like grid-area is
	apparently somewhat inconsistent, so I'm using a custom property instead.
	It should be set to the same thing as grid-area on each input.
	*/
	const gridArea = getComputedStyle(input).getPropertyValue(hfGridAreaProperty);
	if(gridArea) {
		/** @type {HTMLElement} */
		let wrapper = input.nextElementSibling;
		if(wrapper) {
			if(!wrapper.classList.contains(hfWarningGridWrapperClass)) {
				wrapper = undefined;
			}
		}
		if(!wrapper) {
			wrapper = document.createElement('div');
			wrapper.className = hfWarningGridWrapperClass;
			wrapper.role = 'presentation';
			wrapper.style.gridArea = gridArea;
			input.parentNode.insertBefore(wrapper, input.nextSibling);
		}
		wrapper.insertBefore(warning, null);
	} else {
		// Not in a grid, so just insert it in the default location
		input.parentNode.insertBefore(warning, input.nextSibling);
	}
});

CSS

.hfWarningGridWrapper {
	position: relative;
	pointer-events: none;
	align-self: stretch;
	justify-self: stretch;
}

/*
This is copied from the official hyperform.css and adapted for use with the grid wrappers.
I'm not splitting the .hf-invalid and :invalid rules like that does, however, since IE9 doesn't support grid anyway.
*/
.hf-invalid + .hfWarningGridWrapper .hf-warning,
:invalid + .hfWarningGridWrapper .hf-warning {
	display: none;
	position: absolute;
	top: 100%;
	left: 0;
}
.hf-invalid:focus + .hfWarningGridWrapper .hf-warning,
:invalid:focus + .hfWarningGridWrapper .hf-warning {
	display: block;
}

/* Everything below this point is example code that goes with the HTML above */
#someForm {
	display: grid;
	grid-template:
		"nameLabel	name"	auto
		"emailLabel	email"	auto /
		auto		1fr
	;
}
label[for="name"] {
	grid-area: nameLabel;
	--hf-grid-area: nameLabel;
}
#name {
	grid-area: name;
	--hf-grid-area: name;
}
label[for="email"] {
	grid-area: emailLabel;
	--hf-grid-area: emailLabel;
}
#email {
	grid-area: email;
	--hf-grid-area: email;
}

from hyperform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.